0

I run into some silly problem. I got response data in JSON.

function(data){
             if (IsJsonString(data) ) { 
                var datastuff = JSON.parse(data);
            } else { 
                var datastuff = data;
                }

where datastuff is like:

datastuff.id1 = XX;

datastuff.id2 = YY;

etc..

and I need to loop through it. I am trying something like that:

for (i=1;i<10;++i) {
  if (datastuff.id[i]) { some stuff here }
}

and it is not working

if I use

datastuff.id + i 

the same

When I use

datastuff.id1 

it works as expected

So I quess the problem is how to write an iteration value in variable name of JSON parsed objects

Would appreciate any help

Solution was - as Diego suggested:

for (i=1;i<10;++i) {
  if (datastuff[id${id}]) { some stuff here }
}
  • if you want to access to a property using a string you can do `object[key]` .. so in your case `for(let i=1;i<10;i++){datastuff[\`id${i}\`]}` – Diego D Nov 10 '22 at 11:07

0 Answers0