Home > Enterprise >  Accessing array element from API data
Accessing array element from API data

Time:12-12

so maybe you've heard of Splinterlands. This is from a bot that I'm trying to customize, but JS is so confusing to me. When I display the array with JSON.stringify(teamToPlay), I get this:

{ 
  "summoner": "167",
  "cards": [167, 162, 192, "", "", "", "", "fire", "", ""]
}

I'm trying to pull out "fire", the 8th element in the cards array. "cards" is an array, right? Appreciate your help.

CodePudding user response:

Let me explain you with another example

const info = {
    firstName: "priyam",
    lastName: "shah",
    birthYear: 1000,
};
console.log(info.firstName);
console.log(info["lastName"]);

This is how you can access objects. Now it's quite easy to find the 8th character from the array right ? arrName[index]

So it should be obj.cards[7] assuming you save this data coming from api to a variable called obj.

  • Related