Objects with the same content are not equal, that is to say comparing objects with equality is always different unless they are a reference to each other.
This happens because each object is a different instance.
To clarify:
{foo: '123'} == {foo: '123'} // false
{foo: '123'} === {foo: '123'} // false
const foo = {foo: '123'};
const foo2 = foo;
foo === foo2 // true
JSON.stringify({foo: '123'}) === JSON.stringify({foo: '123'}) // true
That is, in your case you will have to compare by strings, or search for properties values of the object like the G. Bittencourt referred.
In principle you could compare strings, as in the last example above... but this can give problems if the order of the properties is different. I suggest you use a function that compares properties on a given object, but you should keep in mind that if there are 2 objects with the same properties it will return the first one you find.
Using the .findIndex
and passing the check to your callback:
let list = [
{nome: 'joão', idade: 15},
{nome: 'pedro', idade: 17},
{nome: 'felipe', idade: 12}
];
const getIndexOfObject = (arr, ...props) => arr.findIndex(
el => props.every(([key, value]) => el[key] === value)
);
const index = getIndexOfObject(list, ['nome', 'pedro'], ['idade', 17]);
console.log(index); // 1
Finally a reply that worked! Thank you very much.
– Only_A_Perfil