I am sorry this seems like an easy answer but I am not really good at programming, I am trying to get a value from an API, I can get the JSON but I only need one value and the other is unnecessary, this is the JSON: https://www.dnd5eapi.co/api/monsters/ =>
"results": [
{
"index": "aboleth",
"name": "Aboleth",
"url": "/api/monsters/aboleth"
},
{
"index": "acolyte",
"name": "Acolyte",
"url": "/api/monsters/acolyte"
},
{
"index": "adult-black-dragon",
"name": "Adult Black Dragon",
"url": "/api/monsters/adult-black-dragon"
}]
and so on,
I am only trying to get the index of each one.
Thank you in advance.
You can convert it to an object pull the data that you need.
const data = JSON.parse(results) // Assuming the variable containing your json is called results
// Make an array of only the indexes
const indexes = data.results.map(v => v.index)
Update: OP Requested
const uri = "https://www.dnd5eapi.co/api/monsters/";
// Promises
fetch(uri)
.then((res) => res.json())
.then((data) => {
const indexes = data.results.map((v) => v.index);
console.log(indexes);
});
// Async Await
async function getData() {
const res = await fetch(uri);
const data = await res.json();
const indexes = data.results.map((v) => v.index);
console.log(indexes);
}
getData()