Tuesday, 6 June 2017

Access index property of JSON in javascript using a variable

The api that i am messing with has a limit of 100 items per request. So I am trying to find out the length variable (which works) but why cant i use that variable to get the index. like below.

   var ttl = results.data.length;
   const s = results.data[ttl].id;

console reads

  TypeError: Cannot read property 'id' of undefined

ttl does not work. But if i do the following...

   const s = results.data[100].id;

the value is recognized fine... But i dont know what the length will be so i need to set it in a var and try to navigate through the JSON with the length var?



via Omar

No comments:

Post a Comment