I am building a feature that can allow users to type a word, select the language the word is in and then get the word's definition and an example using this API service.
The data I want to fetch is whatever it is at position 0 of "exclamation" of "meaning". This way, I can make sure users get the definition and an example for the word they are looking for. My question is, the position of data of "definition" and "example" varies with different languages. As you can see, the one below is the English word's JSON data format.
[{
"word": "hello",
"phonetic": [
"həˈləʊ",
"hɛˈləʊ"
],
"meaning": {
"exclamation": [
{
"definition": "used as a greeting or to begin a telephone conversation.",
"example": "hello there, Katie!"
}
]
}
}]
And this one below is the French word's JSON data format.
[
{
"word": "bonjour",
"phonetic": "",
"origin": "",
"meaning": {
"nom masculin": {
"definitions": [
{
"definition": "Souhait de bonne journée (adressé en arrivant, en rencontrant).",
"example": "",
"synonyms": [
"salut"
]
}
]
}
}
}
]
Currently, I am storing each data's position in a variable and doing this repeatedly until I get to the position of the ultimate data I am trying to fetch like below.
const firstData = data[0];
const firstDataKeys = Object.keys(firstData);
const meaningsIndex = firstDataKeys.indexOf("meaning");
const meaningsData = firstData[firstDataKeys[meaningsIndex]]
const meaningsKeys = Object.keys(meaningsData);
I would like to know if there is any other way to dynamically fetch data when the desired data's position vary with each request.