4
I have the following matrix:
var usuarios = [
{nome: "João", id: 1},
{nome: "Maria", id: 2},
{nome: "José", id: 3},
{nome: "Ana", id: 4},
];
I need to return the José user index. I tried using the index as follows:
var usuarios = [
{nome: "João", id: 1},
{nome: "Maria", id: 2},
{nome: "José", id: 3},
{nome: "Ana", id: 4},
];
console.log(usuarios.indexOf({nome: "José", id: 3}))
But I got back -1. I know it’s possible to do this with the for(), but it’s possible to do it with the index?
could also use the
console.log(usuarios.map(o => o.nome).indexOf("José"))
as well as within Function, right? https://codepen.io/flourigh/pen/NWPVeY– flourigh
@flourigh with modern Javascript can do directly
usuarios.findIndex(o => o.nome === 'José');
– Sergio
in fact, then why preferred the format of his reply?
– flourigh
@flourigh because my answer is almost 4 years old and at the time there was no support for this method, it was released with the ES6 version of Javascript.
– Sergio
For, now that I have seen the date, such a question has appeared in the feed for having its answer edited, because someone edits a 4 year answer
– flourigh