【发布时间】:2021-01-11 17:55:43
【问题描述】:
以下是我从服务器收到的 JSON。
现在我有变量public checkId: any = 54
如何从JSON下面提取ID = 54对应的数据??
我想提取下面提到的针对 KEY 54
"Increase Again": true,
"Decrease Previous" : true,
"Like" : true,
"Dislike" : true,
"Old Again" : false,
"Others" : true
以下是我必须从中提取上述数据的 JSON
{
"27" : {
"Increase": true,
"Decrease" : true,
"Like" : true,
"Dislike" : true,
"Old" : false,
"Others" : true
},
"54" : {
"Increase Again": true,
"Decrease Previous" : true,
"Like" : true,
"Dislike" : true,
"Old Again" : false,
"Others" : true
},
"104" : {
"Increase Previous": true,
"Decrease Prior" : true,
"Like" : true,
"Dislike" : true,
"Old" : false,
"Others Check" : true
}
}
提前致谢...
#Edit - 关键不只是 1,2,3 它们是特定的 ID 53,54,55 也可以是 100, 108, 4657
【问题讨论】:
-
由于 ID 是键,假设 API 中的对象分配给
response变量,您可以简单地执行response[this.checkId]。 -
@MichaelD - 我已经对响应进行了编辑,密钥不是 1、2、3,它可以是任何数字。在那种情况下,你的解决方案会起作用吗??
-
我不明白为什么不这样做。从应用程序的角度来看,b/n 2 或 54 或任何其他数字没有区别。只要该属性可用,它将返回一个有效对象。如果它不可用,它将返回
undefined。
标签: javascript arrays json angular typescript