Retrieving data from API to access array element

In order to determine the object’s type, it is important to understand that objects are essentially associative arrays. With that in mind, how can I access an object using data from an API response?


Question:

Perhaps you’re familiar with Splinterlands. I’m currently attempting to customize a bot, but I find JS to be quite perplexing. Upon using JSON.stringify(teamToPlay), the following result is displayed.

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

Could you assist me in retrieving the 8th element, which is “fire”, from the array called “cards”?


Solution:

Allow me to illustrate with an alternative example.

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

Here is a simple way to retrieve the 8th character from the array using the syntax arrName[index].

Assuming the data fetched from the API is saved in a variable named “obj”, the code should be

obj.cards[7]

.

Frequently Asked Questions