-1

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.

Olipol
  • 315
  • 1
  • 9
  • 19
  • 2
    `data` is an object.. you should use `for (var key in data) { ... }` – Washington Guedes Jun 14 '17 at 15:24
  • The only thing that the output from `JSON.stringify` should ever be passed to is a JSON decoder. Why are you taking a perfectly good data structure and then trying to parse its serialised form? – Alnitak Jun 14 '17 at 15:25
  • I am using JSON.stringify because if I console.log data I just get Object Object – Olipol Jun 14 '17 at 15:26
  • @Olipol — Converting an object to a string of JSON it not remotely like converting it to an array. – Quentin Jun 14 '17 at 15:27
  • @Olipol If you want the string representation of your object to display in debugging, then sure, `stringify` will do that, but if you want a useful data structure with properties, don't turn it into a string (or at least don't assume it will still be a useful data structure after you turn it into a string). – apsillers Jun 14 '17 at 15:28
  • Try Changing this `$('#img-thumb-id'+i).attr('src', data[i]);` to `$('#img-thumb-id'+i).attr('src', data[i.toString()]);` – Hassan Imam Jun 14 '17 at 15:28
  • @Olipol fine, but you don't want to use that same string output for actually working on the data – Alnitak Jun 14 '17 at 15:29
  • I dunno `for (var i = 0; i < data.length; i++) { ... `did not work ` – Olipol Jun 14 '17 at 15:31
  • Related: [Access / process (nested) objects, arrays or JSON](https://stackoverflow.com/q/11922383/218196). – Felix Kling Jun 14 '17 at 15:44

2 Answers2

6

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.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • hi thanks how would the Loop look like, just a Basic Loop through that data, earlier I tried to Loop through data.length but that did not work aswell – Olipol Jun 14 '17 at 15:28
  • @Olipol — See the last paragraph of this answer (including a link to a whole question about that). – Quentin Jun 14 '17 at 15:34
0

In order to loop over object keys you can use:

var keys = Object.keys(data);

Which gives you an array with all the keys in your object. Now you can loop the values by:

for (var i = 0; i < keys.length; i++) {
   var value = data[key];
   // your code here...
}
Elad Douenias
  • 208
  • 2
  • 4