我有一个包含objects数组的对象。鉴于:
var storedetails = {
"stores": {
"store1": [{
"pno": 1,
"pname": "Lux",
"stock": "100",
"cost": 10,
"discount": 10,
"details": "this is lux"
},
{
"pno": 2,
"pname": "Rexona",
"stock": "200",
"cost": 20,
"discount": 20,
"details": "this is Rexona"
}
],
"store2": [{
"pno": 3,
"pname": "Dove",
"stock": "300",
"cost": 30,
"discount": 30,
"details": "this is Dove"
},
{
"pno": 4,
"pname": "Lifebouy",
"stock": "400",
"cost": 40,
"discount": 40,
"details": "this is LifeBouy"
}
],
"store3": [{
"pno": 5,
"pname": "Dettol",
"stock": "300",
"cost": 50,
"discount": 50,
"details": "this is Dettol"
}]
}
}
我希望基于PNO(产品编号),需要检索对象并将其渲染到AngularJS页面。如何检索该对象?
答案 0 :(得分:0)
围绕对象的简单循环可以做到。
var t = storedetails.stores;
function getProduct(storeName, pId){
var p;
angular.forEach(t[storeName], function(product, index){
console.log(product.pno, pId);
if(product.pno === pId)
{
console.log("===========");
p=product;
}
})
return p;
}
现在只调用函数getProduct("store1", 1)
将从store1对象中提供产品ID 1的结果
更新
function getProduct(pId){
var p;
angular.forEach(t, function(storeName, i){
angular.forEach(storeName, function(product, index){
if(product.pno === pId)
{
p=product;
}
})
})
return p;
}
答案 1 :(得分:0)
function getProd(pId) {
var p;
angular.forEach($rootScope.storedetails, function (storeName, i) {
angular.forEach(storeName, function (product, index) {
angular.forEach(product, function (productdet, index) {
//debugger
if (productdet.pno === pId) {
p = productdet;
}
})
})
})
return p;
}