1

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

Angela Roux
  • 473
  • 1
  • 5
  • 14

4 Answers4

2

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);
Sushanth --
  • 55,259
  • 9
  • 66
  • 105
  • The poster specifies that they are seeking "the index of that object." `find` returns the object itself. – aaplmath Aug 01 '18 at 01:14
  • Seems like the id in the example is a bit misleading. What if there is nothing like a unique identifier. I just want to compare everything and return the index where every property is equal? – Angela Roux Aug 01 '18 at 05:02
  • 1
    @AngelaRoux You should then change a logic of the application as it will create more and more issues as you go. If you really need to, this [question](https://stackoverflow.com/questions/201183/how-to-determine-equality-for-two-javascript-objects) might come handy. – Dawid Zbiński Aug 01 '18 at 06:00
0

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.

aaplmath
  • 1,717
  • 1
  • 14
  • 27
  • findIndex doesn't work in IE 11 and also what if the id is not a unique identifier, I want to compare all of the properties in the object with the object passed in and return the index if everything is a match. – Angela Roux Aug 01 '18 at 05:03
0

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
Akrion
  • 18,117
  • 1
  • 34
  • 54
0

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);
sakthi
  • 929
  • 6
  • 18