Nested forEach loop does not work

后端 未结 3 1885
臣服心动
臣服心动 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 03:01

    Nesting foreach is really a bad practice. Instead of that you can use the map() function to get data.

    Suppose a array of object be like this & now here how to use map instead of multiple foreach();

    data = [{
        dataModels: [{
            childNodes: {
                appId: 'foo'
            }
        }]
    }];
    
    
    data.forEach(function(obj) {
        var res = obj.dataModels.map(function(o) {
            return o.childNodes;
        });
        console.log(res[0]);
    });

提交回复
热议问题