1
I have a json where I need to return the object based on a condition.
I need to return the corresponding object to the artist selected by the user, being it selected by name or nickname.
The search for the name I’m managing to do, but I can’t succeed for the search to be done by nickname. Any suggestions on how I do it?
var objeto = [
{
"apelido": ["Lukas", "Vintage", "Vintage Culture"],
"nome": "Vintage Culture",
"title": "Cassian - Magical ft. Zolly (Vintage Culture Remix)",
"link": "https://www.youtube.com/watch?v=FxDVs4i6XJg"
},
{
"apelido": ["Illusionize", "Pedrinho", "Pedro"],
"nome": "Illusionize",
"title": "Illusionize - Down",
"link": "https://www.youtube.com/watch?v=2ea9d-FgQ-I"
},
];
var resultado = [];
function percorrer(obj) {
for (var propriedade in obj) {
if (obj.hasOwnProperty(propriedade)) {
if (typeof obj[propriedade] == "object") {
percorrer(obj[propriedade]);
} else {
resultado.push(obj[propriedade]);
}
}
}
}
percorrer(objeto);
var artistaSolicitado = "Lukas";
const result = objeto.find( data => data.nome === artistaSolicitado );
if (!result) {
console.log('não achou pelo apelido');
} else {
titleArtist = result.title,
linkVideos = result.link
} ;
console.log(result)
});
Sensational Miguel, your solution worked perfectly. Thank you!
– Kelvin Carvalho
You’re welcome @Kelvincarvalho, thank goodness.
– Miguel