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
MarkBoots10mo 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ë10mo 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
glutonium10mo 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
MarkBoots10mo ago
here it will only return the first result
Pi, a future fluent jp speaker
Yeah I need all of them
glutonium
glutonium10mo ago
aah i see hmm
Want results from more Discord servers?
Add your server