I'm trying to get reandom info frinm the API of Jojo's. I want to select specific parts of the array that i get from the API so that i show only the useful information that it provides from the characters. But i cant manage to get the info of the part from "stand_type" o any specific section of the array, please someone help me with this.
My Code so far:
async function getCharacter(){
const number = getRandomNumber();
var infos = await
fetch('https://jojoapi.herokuapp.com/DiamondIsUnbreakable').then(response => response.json());
console.log("Number = " number);
console.log(infos[number])}
CodePudding user response:
infos[number] is accessing the object of result with index is the random number. So you need to access the particular key by passing the key name like
infos[number]["stand_type"]
infos[number].stand_type
And I hope, that generating a random number function of your gives a number less than the array length. I modified the code a bit and it works as expected(I guess)
async function getCharacter() {
var url = 'https://jojoapi.herokuapp.com/DiamondIsUnbreakable';
var infos = await fetch(url).then(response => response.json());
const number = Math.floor(Math.random()*infos.length);
console.log("Number = " number);
console.log(infos[number]["stand_type"])
}
CodePudding user response:
It looks like you were on the right path. What I do to debug API's is printing the response first and understanding the path to the variable I want. When I did that I found out that I can get 'stand_type' easily.
async function getCharacter(){
const number = 1; // I put 1, however any integer should work
var infos = await
fetch('https://jojoapi.herokuapp.com/DiamondIsUnbreakable').then(response => response.json());
console.log("Number = " number);
// console.log(infos);
console.log(infos[number].stand_type); // stand_type can be any other variable retrieved from API
}