简体   繁体   中英

Javascript loop through JSON arrays

Im trying to loop through a JSON object and gab the value bingo m having trouble

{"testList":[{"number":"107832","secondList":[{"thirdList":[{"blah":"11111","blah2":"222222"}],"bingo":"0000"}]}]}

Its apart of 'secondList', Ijust dont know how to access without a nested loop.

for(var i=0;data.testList.length<1;i++){

    var fooObject = { 


            "number": data.testList[i].number,
            "bingo": <<<-----How to get this value???


    };

data.testList[i].secondList[0].bingo是您想要的。

All of the arrays in this object are of length 1. If that will always be the case, then you don't have to loop through them:

fooObject = {
    "number" : data.testList[0].number,
    "bingo" : data.testList[0].secondList[0].bingo
};

If you have more than 1 item in the arrays and need to loop, you just need to loop through secondlist as well. Inside your for loop:

for (var j = 0; data.testList[i].secondList.length < 1; i++) {
    fooObject.bingo = data.testList[i].secondList[j].bingo;
}

this is what you need:

console.log( json.testList[0].secondList[0].bingo)

http://fiddle.jshell.net/YA93Z/

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