Nested forEach loop does not work

后端 未结 3 1881
臣服心动
臣服心动 2021-02-08 02:22

I have some data that is in JSON object array. I\'m trying to use nested forEach loops to extract the data.

The data is modeled like belo. There\'s multiple dataModels

3条回答
  •  独厮守ぢ
    2021-02-08 02:49

    You are not targeting the array inside the entry object, you need to loop over the childNodes property in order to get the data you want. See example below.

    var dataModels = [];
    
    dataModels[0] = {
        childNodes: []
    };
    
    dataModels[0].childNodes[0] = {
        appId: "foo"
    };
    
    dataModels.forEach(function(entry){ 
        entry.childNodes.forEach(function(childrenEntry) { // was missing a )
          console.log(childrenEntry.appId);
        });
    });
    

    JsFiddle demo

提交回复
热议问题