假设我有一个如下所示的 JSON 对象,称为“价格”
"Scrap Metal": {
"defindex": [
5000
],
"prices": {
"3": {
"Tradable": {
"Craftable": {
"0": {
"currency": "keys",
"value": 432,
"last_update": 1425334795,
"difference": 3491.64
}
}
}
},
"6": {
"Tradable": {
"Craftable": {
"0": {
"currency": "metal",
"value": 0.11,
"last_update": 1336410088,
"difference": 0
}
}
}
}
}
},
这样继续下去,具有相似的结构但不同的值。我想搜索 defindex: 5000
值,然后从中获取 6
中的 value
。换句话说,如果我通过搜索 defindex 知道它在代码中的位置,我会调用 "name".prices[6].Tradable.Craftable[0].value
。
我尝试使用underscore.js为此,但没有成功。我的代码是:
_.where(prices, { defindex: '5000' });
如有任何帮助,我们将不胜感激。谢谢!
最佳答案
我会使用#find,它允许您使用一个函数,迭代每个键/值对,其中值将是顶部键指向的值。
_.find(prices, function(value, key) {
return (value.defindex == "5000")
})
关于JavaScript 在 json 中搜索,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31576350/