Angela Roux
Angela Roux

Reputation: 473

How to get the index of an object in an array of objects when it matches completely javascript

I have an array of objects and I would like to get the index of the object in the array when I get a match.

I have the array as follows:

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}},
 .....
];

Currently I am using indexOf which worked initially and now it doesn't work properly. It returns -1.

let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = x.indexOf(find); // should return 1.

The whole should match in the array and should return the index of that object. How can I achieve this? Should I be using some() ?

Thank you

Upvotes: 1

Views: 78

Answers (4)

Sushanth --
Sushanth --

Reputation: 55750

You can use .find instead of indexOf as 2 objects are never equal ( as they point to different reference in memory ) which is what you seem to pass as an argument.

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}}
];

let found = x.find(function(item) {
  // you can use the condition here
  return item.info.id === 564;
});

console.log(found);

To find the index, you can use .findIndex method instead.

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}}
];

let foundIndex = x.findIndex(function(item) {
  // you can use the condition here
  return item.info.id === 564;
});

console.log(foundIndex);

Upvotes: 2

sakthi
sakthi

Reputation: 929

You can make use of underscore _.isEqual for Object comparison and some() or any looping mechanism to iterate the array.

let iFoundIndex = -1;
let bFound = x.some((data,index) => {
                    if(_.isEqual(data,find){
                        iFoundIndex = index;
                        return true;
                    }
                    return false;
                  }
 //console.log(iFoundIndex);

Upvotes: 0

Akrion
Akrion

Reputation: 18515

If we live in the _.lodash world than this works since lodash would go deep on objects:

let data = [
{name: "emily", info: { id: 123, gender: "female", age: 25}},
{name: "maggie", info: { id: 234, age: 22, gender: "female"}},
{name: "kristy", info: { id: 564, gender: "female", age: 26}},
];

let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = _.findIndex(data, (i) => _.isEqual(i, find))

console.log(index)  // 1
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.10/lodash.min.js"></script>

A more brutish approach which obviously it is not performant and as pointed out wont work if the order of the props is different.

let data = [
{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}},
];

var objectJSONs = data.map((i) => JSON.stringify(i))

let myJSON = JSON.stringify({name: "maggie", info: { id: 234, gender: "female", age: 22}});
let index = objectJSONs.indexOf(myJSON)

console.log(index) // 1

Upvotes: 0

aaplmath
aaplmath

Reputation: 1807

Objects cannot be compared by traditional equality in JavaScript. Instead, use the ES6 findIndex method to compare each object's properties with the desired values. Here is an example:

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}}
];
let find = {name: "maggie", info: { id: 234, gender: "female", age: 22}};
let index = x.findIndex(element => element.info.id === find.info.id); // index === 1

The id value seems to be sufficient to identify an object in your scenario; if you need to compare more properties, you could obviously add additional equality checks (e.g., element.name === find.name) with the && operator.

Upvotes: 0

Related Questions