JavaScript loop through json array

If you are not interested in support for old browsers, the best way to browse an array of json is:

var dotArrayJSON = [
                    {"label": 1}, 
                    {"label": 2}, 
                    {"label": 3}, 
                    {"label": 4}
                   ];
dotArrayJSON.forEach(loopThrough);

function loopThrough(JSON) {
    console.log(JSON.label)
}

Otherwise with a simple for:

for(var i = 0; i < dotArrayJSON.length; i++) {
    var obj = dotArrayJSON[i];

    console.log(obj.label);
}

Leave a Comment

Your email address will not be published. Required fields are marked *