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

2

4 Answers 4

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);

Sign up to request clarification or add additional context in comments.

3 Comments

The poster specifies that they are seeking "the index of that object." find returns the object itself.
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?
@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 might come handy.
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.

1 Comment

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.
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

3 Comments

Won't work if the order of the properties are changed!
That is true. Good call.
Added a lodash solution which works regardless of the order. Now it is not vanilla JS etc but he should have as many options as possible to chose from :)
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);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.