无法找到正确的工作流程来从循环数组中获取以下内容:
warehouse
值warehouse
值的对象并在其中返回price
值例如:如果我要搜索1374610389
仓库价值,我会得到78.00
var test = [
{
lorem: "ipsum",
dolor: "sit",
price: "10.00",
warehouse: 1157964289
},
{
lorem: "ipsum",
dolor: "sit",
price: "22.00",
warehouse: 1269753487
},
{
lorem: "ipsum",
dolor: "sit",
price: "78.00",
warehouse: 1374610389
},
{
lorem: "ipsum",
dolor: "sit",
price: "32.00",
warehouse: 1674985630
},
{
lorem: "ipsum",
dolor: "sit",
price: "16.00",
warehouse: 1847893458
}
]

答案 0 :(得分:2)
find()方法返回数组中第一个满足提供的测试函数的元素的值。
您可以使用 find
方法:
test.find(obj => obj.warehouse == '1374610389').price
var test = [
{
lorem: "ipsum",
dolor: "sit",
price: "10.00",
warehouse: 1157964289
},
{
lorem: "ipsum",
dolor: "sit",
price: "22.00",
warehouse: 1269753487
},
{
lorem: "ipsum",
dolor: "sit",
price: "78.00",
warehouse: 1374610389
},
{
lorem: "ipsum",
dolor: "sit",
price: "32.00",
warehouse: 1674985630
},
{
lorem: "ipsum",
dolor: "sit",
price: "16.00",
warehouse: 1847893458
}
]
console.log( test.find(obj => obj.warehouse == '1374610389').price);
答案 1 :(得分:1)
var test = [{
lorem: "ipsum",
dolor: "sit",
price: "10.00",
warehouse: 1157964289
}, {
lorem: "ipsum",
dolor: "sit",
price: "22.00",
warehouse: 1269753487
}, {
lorem: "ipsum",
dolor: "sit",
price: "78.00",
warehouse: 1374610389
}, {
lorem: "ipsum",
dolor: "sit",
price: "32.00",
warehouse: 1674985630
}, {
lorem: "ipsum",
dolor: "sit",
price: "16.00",
warehouse: 1847893458
}];
for (var i = 0; i < test.length; i++) {
if (test[i].warehouse == 1374610389) {
console.log(test[i].price);
}
}
使用length属性获取数组的长度,并使用for
对其进行枚举。
同时检查Zakaria Acharki的回答,他的方式更优雅。