Return array object with condition

Asked

Viewed 47 times

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)

});


1 answer

1


You can do it like this:

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 search = window.prompt('insira nome ou apelido');
var person = objeto.find((item, idx) => item.apelido.map((item) => item.toLowerCase()).indexOf(search.toLowerCase()) !== -1 || item.nome.toLowerCase() === search.toLowerCase());

console.log(person); // caso seja "undefined" é porque não encontrou

Note that the above solution transforms the strings to lowercase (.toLowerCase()) for the search to find cases like "Ukas", but if you want the search to be exactly the same as the one you write you can change to:

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 search = window.prompt('insira nome ou apelido');
var person = objeto.find((item, idx) => item.apelido.indexOf(search) !== -1 || item.nome === search);

console.log(person); // caso seja "undefined" é porque não encontrou

  • 1

    Sensational Miguel, your solution worked perfectly. Thank you!

  • You’re welcome @Kelvincarvalho, thank goodness.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.