Olipol Olipol -4 years ago 48
Javascript Question

JavaScript loop through a JSON.stringify(data)

I want to Loop through my data and do stuff.

Following

var stuff = JSON.stringify(data)


Returns me something like this:

{"0":"data:image/png;base64,testi,"2":"data:image/png;base64, testi2, ....


I Need to Loop through that but the Approach I did, did not work.

for (var i = 0; i < stuff.length; i++) {
$('#img-thumb-id'+i).attr('src', data[i]);
}


Edit

I am using
JSON.stringify
because
console.log(data)
just returned me object object which I cant work with.

Answer Source

If you want to loop through data, then you need to loop through data.

Converting it to a JSON document will give you a string. You can't (usefully) loop through that.

Since converting it to JSON shows that it is an object, not an array, it is unlikely to have a length, so you'll need to use a method to loop over objects.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download