Fetching a word from this API( https://api.dicionario-aberto.net/random ) gives me the whole object:
{
"word": "linguaraz",
"wid": 74891,
"sense": 1
}
How can I get to return only the word value in this object?
This is my code:
const getWord = async function () {
const response = await fetch ("https://api.dicionario-aberto.net/random?get.value")
const words = await response.text();
const wordArray = words.split("\n");
const randomIndex = Math.floor(Math.random() * wordArray.length);
word = wordArray[randomIndex].trim();
placeholder(word);
};
You need to convert your api result into JSON first instead of text, then it will be easier to access word, like this
async function getData(){
res = await fetch('https://api.dicionario-aberto.net/random');
data = await res.json();
console.log(data.word);
}
getData();
Convert fetch data to json and read it as object property:
const getWord = async function () {
const response = await fetch ("https://api.dicionario-aberto.net/random?get.value")
const words = await response.json();
placeholder(word);
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.