简体   繁体   中英

How do I iterate through nested properties of an json object and create a new array list?

I have an json object 'items' and i'm trying to get all the 'values' into a separate string:

 "items":[  
      {  
         "id":0,
         "categoryId":0,
         "label":"TOTAL EDUCATION",
         "total":739599,
         "values":[  
            451383,
            288216
         ],
         "items":[  ],
         "metadataIds":"20006",
         "collapsed":true
      },
      {  
         "id":0,
         "categoryId":0,
         "label":"TOTAL HIGHWAYS",
         "total":63678,
         "values":[  
            32672,
            31006
         ],
         "items":[  ],
         "metadataIds":"20022",
         "collapsed":true
      },

for (var i = 0; i < obj.items.length; i++) {
        var cost = obj.items[i].values;
    }

the output i'm trying to achieve from the values:

[451383,288216],[32672,31006] etc.

Example here: https://jsfiddle.net/zidski/6mg8fszj/

Currently I can only output 1 of the 'values' set.

Currently I can only output 1 of the 'values' set.

Well, yes; you're overwriting it every time.

You've said you want "one string," and the output you've shown has [ and ] in it. Amusingly, while you're not dealing with JSON when iterating, JSON can play a role in producing that string:

var str = obj.items.map(function(item) {
    return JSON.stringify(item.values);
}).join(",");

Array#map loops through items building a new array out of what we return for each item from our callback; in the above, we're returning the JSON string equivalent of each item's values array. Then we join the resulting array with commas in-between to get the final string.

Example:

 var obj = { "items": [{ "id": 0, "categoryId": 0, "label": "TOTAL EDUCATION", "total": 739599, "values": [ 451383, 288216 ], "items": [], "metadataIds": "20006", "collapsed": true }, { "id": 0, "categoryId": 0, "label": "TOTAL HIGHWAYS", "total": 63678, "values": [ 32672, 31006 ], "items": [], "metadataIds": "20022", "collapsed": true }] }; var str = obj.items.map(function(item) { return JSON.stringify(item.values); }).join(","); document.body.innerHTML = "<pre>" + str + "</pre>"; 

If you don't actually want a string, but instead you want an array of arrays, it's a bit simpler:

var arrayOfArrays = obj.items.map(function(item) {
    return item.values;
});

 var obj = { "items": [{ "id": 0, "categoryId": 0, "label": "TOTAL EDUCATION", "total": 739599, "values": [ 451383, 288216 ], "items": [], "metadataIds": "20006", "collapsed": true }, { "id": 0, "categoryId": 0, "label": "TOTAL HIGHWAYS", "total": 63678, "values": [ 32672, 31006 ], "items": [], "metadataIds": "20022", "collapsed": true }] }; var arrayOfArrays = obj.items.map(function(item) { return item.values; }); // Just using JSON so we have text to show as a result document.body.innerHTML = "<pre>" + JSON.stringify(arrayOfArrays, null, 2) + "</pre>"; 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM