0

I have been using nested loops to access data of the json object to display the id and type of topping, however its not working. Here's my code:

    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);

Could someone please highlight the error in above code, Thanks

2
  • why do you try to do this angular.forEach(j_obj, function(an_object){? In think you need just one foreach angular.forEach(j_obj.topping,function(innerobject){ Outer_log.push("ID : "+innerobject.id+" type : "+innerobject.type); },Outer_log); Commented Nov 20, 2016 at 16:05
  • Actually I would be having multiple such objects, hence used an outer loop Commented Nov 22, 2016 at 10:41

3 Answers 3

1

Without using nested loop you can iterate using angular.forEach like this

var finalArray=[];

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

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

});

Sign up to request clarification or add additional context in comments.

Comments

1

Angulars forEach is intended to iterate over arrays not object. so if you change your code to something like this

var j_obj = [{ ...}] //object is wrapped inside array.

it will work. Another thing is you don't need a nested loop in this case. You can just do:

angular.forEach(j_obj.topping, function(key, value){ ... });

Comments

0

you are iterating over object where as loop run over array.

hope this helps JSfiddle link

 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 = [];
 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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.