Better way to return a specific item from json

I just want to return the strings from the definitions.definitions objects... but the way I'm doing it feels kinda not optimized as it is about to become an O(n³). Does anyone know a better way? Here is the link of the api to understand its structure: https://api.dictionaryapi.dev/api/v2/entries/en/oi
No description
6 Replies
MarkBoots
MarkBoots5mo ago
i think that is the way to do it. you could use a regex, but don't know if this introduces other incertainties (i think it will struggle if the definition includes escaped "'s Im not great at it, but came up with this. (it does work with your example data) It matches all texts between "definition":" and "
const data = await response.text();
const definitions = data.match(/(?<=\"definition\":\")(.*?)(?=\")/g);
console.log(definitions)
/*
[
"(sometimes capitalized) A working-class punk rock subgenre of the 1970s, sometimes associated with racism.",
"Said to get someone's attention; hey.",
"An expression of surprise.",
"An informal greeting, similar to hi.",
"An exclamation typically expressing mild frustration or expressing feelings of uncertainty or concern.",
"(representing rural dialect pronunciation) I."
]
*/
const data = await response.text();
const definitions = data.match(/(?<=\"definition\":\")(.*?)(?=\")/g);
console.log(definitions)
/*
[
"(sometimes capitalized) A working-class punk rock subgenre of the 1970s, sometimes associated with racism.",
"Said to get someone's attention; hey.",
"An expression of surprise.",
"An informal greeting, similar to hi.",
"An exclamation typically expressing mild frustration or expressing feelings of uncertainty or concern.",
"(representing rural dialect pronunciation) I."
]
*/
Zoë
Zoë5mo ago
Have you benchmarked how long it takes? O(n^3) doesn’t really matter if n is small or if the tasks are fast Aim for clear readable, editable, code first; if you notice that there is a delay, find out what is causing it. Only then try and optimise
glutonium
glutonium5mo ago
i remember i used this api for my wordle game to check if the users input is valid or not. I also extracted definition. here's how i used it
export default async function isValid(word){
try{
const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await res.json();
if(data.title == "No Definitions Found"){
return {
valid: false,
definition: `No Definitions Found.\nPerhaps check https://www.google.com/search?q=${word}+meaning`
};
}
else{
return {
valid: true,
definition: data[0].meanings[0].definitions[0].definition
};
}
}
catch(err){
console.log(err);
return false;
}
}
export default async function isValid(word){
try{
const res = await fetch(`https://api.dictionaryapi.dev/api/v2/entries/en/${word}`);
const data = await res.json();
if(data.title == "No Definitions Found"){
return {
valid: false,
definition: `No Definitions Found.\nPerhaps check https://www.google.com/search?q=${word}+meaning`
};
}
else{
return {
valid: true,
definition: data[0].meanings[0].definitions[0].definition
};
}
}
catch(err){
console.log(err);
return false;
}
}
MarkBoots
MarkBoots5mo ago
here it will only return the first result
Pi (I'll eat who dont correct me
Yeah I need all of them
glutonium
glutonium5mo ago
aah i see hmm