-4

i have a data response from API.

and how to loop this data, cause i only get first data. and this data not array.

data = [{"gamename":"game 1","gamelink":"link 1","image_link":"image 1","startfrom":"1"},{"gamename":"game 2","gamelink":"link 2","image_link":"imgae 2","startfrom":"2"}]

var json = response.data[0].games;
console.log(json);

if(Array.isArray(json)){
 console.log('array');
}else{
 console.log('not array');
}

for (var i = 0; i < json.length; i++) {
  console.log('gamename : ' +json[i].gamename+ " - game link: " +json[i].image_link);
 }

enter image description here

Tongat
  • 131
  • 7
  • 2
    Why are you wrapping your JSON, which is already an array, in another array? – Robby Cornelissen Jun 08 '20 at 03:47
  • Why do you have array[0][i]? I believe it should be just array[i]. You're pre-fixing the output to be the first element there. – Abhishek Boorugu Jun 08 '20 at 03:47
  • 2
    Does this answer your question? [How to loop through an array containing objects and access their properties](https://stackoverflow.com/questions/16626735/how-to-loop-through-an-array-containing-objects-and-access-their-properties) – ABGR Jun 08 '20 at 03:54
  • there are already `[` and `]` surrounding the two objects. `[ {}, {} ]` is an array of two objects. There is no need to wrap it in another array. – user120242 Jun 08 '20 at 04:06

7 Answers7

1

As @Robby Cornelissen has said, you are wrapping your array in another array, which is unnecessary and breaking your code. Your for loop only iterates over a single element in the outer array.

Also, it is strange for an API response to have a JSON string embedded as a string value within a JSON's property. You should post how you are retrieving the data.

response={data:[{games:`[{"gamename":"game 1","gamelink":"link 1","image_link":"image 1","startfrom":"1"},{"gamename":"game 2","gamelink":"link 2","image_link":"imgae 2","startfrom":"2"}]`}]}

    var json = response.data[0].games;
    console.log(json);
    
    var array = JSON.parse(json);
    console.log(array);
    
    for (var i = 0; i < array.length; i++) {
     console.log('gamename : ' +array[i].gamename+ " - game link: " +array[i].image_link);
    
    }
user120242
  • 14,918
  • 3
  • 38
  • 52
0

you can use array.map(),array.forEach()

0

You may simply use foreach or for(on the base of length) or for of loop Example

 for (let element of data) {
   console.log(element );
}

And to print data in format you wish, use like

for (let element of data) {
   console.log('gamename : ' +element.gamename+ " - game link: " +element.image_link);
}
SayAz
  • 751
  • 1
  • 9
  • 16
0

Try

for(var i =0; i<data.length; i++)
{
    console.log(data[i]['gamename']);
    console.log(data[i]['gamelink']);
}
Tanner Babcock
  • 3,232
  • 6
  • 21
  • 23
Deepak Gupta
  • 614
  • 1
  • 7
  • 14
0
var data = [{"gamename":"game 1","gamelink":"link 1","image_link":"image 1","startfrom":"1"},{"gamename":"game 2","gamelink":"link 2","image_link":"imgae 2","startfrom":"2"}]

as you said if your array look like above one, use below code

data.forEach((el)=>console.log(el))

you will get every single object in that array.

Nagender
  • 47
  • 1
  • 2
  • 6
0

if you are getting this data from a fetch you need to do res.json() in the fetch, an example is:

some data
[
  {"gamename":"game 1","gamelink":"link 1","image_link":"image 1","startfrom":"1"},
  {"gamename":"game 2","gamelink":"link 2","image_link":"imgae 2","startfrom":"2"}
]

fetch("someUrl")
  .then(res => res.json()) // Here you parse the response
  // Here you use forEach to loop in the response, and with destructuring you get the items with the name "gamename" and "gamelink"
  .then(game => game.forEach(({gamename, gamelink}) => console.log(`gamename : ${gamename} - game link: ${gamelink}`)) // Here you log your data, and asumming that this is like the one above, you can do something like this
Oscar Velandia
  • 1,157
  • 1
  • 7
  • 9
-1

I suspect your json is already parsed, and you don't need to re-parse it. A lot of http clients will do this if the content type header is application/json.

Try:

var games = response.data[0].games
for (var i = 0; i < games.length; i++) {
 console.log('gamename : ' +games[i].gamename+ " - game link: " +games[i].image_link);
}
ibash
  • 1,477
  • 17
  • 31