I am working with an array of objects and need to find the index of a specific object within the array when there is a match.
Here is an example of my current array:
let x = [
{name: "emily", info: { id: 123, gender: "female", age: 25}},
{name: "maggie", info: { id: 234, gender: "female", age: 22}},
{name: "kristy", info: { id: 564, gender: "female", age: 26}},
.....
];
Previously, I was using indexOf
to achieve this, but now it is not returning the correct index, instead giving -1
.
let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = x.indexOf(find); // should return 1.
The entire object needs to be matched in order to retrieve the correct index. Do you have any suggestions on how I can accomplish this? Should I consider using some()
? Your guidance would be greatly appreciated.
Thank you