我想直接通过以下数组中的categoryname键获取价格值,而不使用node.js进行循环。有可能吗?
PriceItem [
{ _id: 561e92d7b5737103130a374b,
price: ‘1000',
categoryname: '561e7cbd288906ab3fa3b52b' }
{ _id: 561e92d7b5737103130a374c,
price: ‘2000',
categoryname: '561e7cc3288906ab3fa3b52c' }
{ _id: 561e92d7b5737103130a374d,
price: ‘3000',
categoryname: '561e3c3aabf5d47634c465bd' }
{ _id: 561e92d7b5737103130a374e,
price: ‘4000',
categoryname: '561e3c3eabf5d47634c465be' }
{ _id: 561e92d7b5737103130a374f,
price: ‘5000',
categoryname: '561e6439249dced339d529a1’ }
]
答案 0 :(得分:2)
我认为这是可以接受的:
function getPriceByCategory(categoryName) {
var retVal = '';
for (var i = 0; i < PriceItem.length; i++) {
if (PriceItem[i].categoryname === categoryName) {
retVal = PriceItem[i].price;
break;
}
}
return retVal;
}
另外,我认为上述数据不是一个多维数组,而是一个对象数组。
答案 1 :(得分:1)