gpt4 book ai didi

javascript - angularjs 中的嵌套循环

转载 作者:行者123 更新时间:2023-12-02 13:55:49 26 4
gpt4 key购买 nike

我一直在使用嵌套循环访问 json 对象的数据以显示 toppingidtype,但是它不起作用。这是我的代码:

    var j_obj = {
"id": "0001",
"type": "donut",
"name": "Cake",
"ppu": 0.55,
"batters": {
"batter": [{
"id": "1001",
"type": "Regular"
}, {
"id": "1002",
"type": "Chocolate"
}, {
"id": "1003",
"type": "Blueberry"
}, {
"id": "1004",
"type": "Devil's Food"
}]
},
"topping": [{
"id": "5001",
"type": "None"
}, {
"id": "5002",
"type": "Glazed"
}, {
"id": "5005",
"type": "Sugar"
}, {
"id": "5007",
"type": "Powdered Sugar"
}, {
"id": "5006",
"type": "Chocolate with Sprinkles"
}, {
"id": "5003",
"type": "Chocolate"
}, {
"id": "5004",
"type": "Maple"
}]
}
var Outer_log=[];
debugger
angular.forEach(j_obj, function(an_object){
//Outer_log.push("ID : "+an_object.id+" type : "+an_object.type);
angular.forEach(an_object.topping,function(innerobject){
Outer_log.push("ID : "+innerobject.id+" type : "+innerobject.type);
},Outer_log);
});
console.log(Outer_log);

有人可以指出上面代码中的错误吗,谢谢

最佳答案

不使用嵌套循环,您可以像这样使用 angular.forEach 进行迭代

var finalArray=[];

angular.forEach(j_obj[0].topping, 函数(eachobject){

finalArray.push("ID : "+ eachobject.id+" type : "+ eachobject.type);

});

关于javascript - angularjs 中的嵌套循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40706167/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com