klonaway
klonaway

Reputation: 479

How can I find the index of an object having a specific value in an array?

Since the title is not easy to get, I'll provide an example :

var myObject = {
  "myArray": [{
"hashKey": "someHashkey1",
"data": "someData1"
  }, {
"hashKey": "someHashkey2",
"data": "someData2"
  }, {
"hashKey": "someHashkey3",
"data": "someData3"
  }]
};
        

I have "myObject" containing "myArray", and now I would like to efficiently find the index of the object having, for instance, the hashKey "someHashKey2".

I could build my own loop to check all elements in the array, but :

  1. is there a built-in way to do it ? Something like indexOf() ?

  2. is there a npm tool for that kind of need (I work in Node.js)

  3. is there an efficient way to work here ? some bad way to avoid ?

[EDIT :] My efficiency question is due to the fact that I will have to do this operation for basically all the hashKeys. I just found this question which has a "lookup" answer that could help. Still in the process of understanding it all...

Upvotes: 1

Views: 282

Answers (3)

Shlomi Haver
Shlomi Haver

Reputation: 974

If you have an hash key I would recommend you to convert the array to an object and then you could find the object using the hash key like this: myObject.myArray["someHashkey1"]

var myObject = 
    { "myArray" : 
       {
         "someHashkey1":{
          "hashKey": "someHashkey1",
          "data": "someData1"
         },
         "someHashkey2":{
          "hashKey": "someHashkey2",
          "data": "someData2"
         }
       }
     };
     console.log(myObject.myArray["someHashkey1"]);

Here is a small performance test for array vs hash object as you can see there is a small difference between finding the same key in an array in an object. the object is a bit faster.

Upvotes: 0

Morteza Tourani
Morteza Tourani

Reputation: 3536

You can simply use Array.prototype.findIndex to get index of every item you want.

var myObject = {"myArray": [{"hashKey": "someHashkey1", "data": "someData1"}, {"hashKey": "someHashkey2", "data": "someData2"}, {"hashKey": "someHashkey3", "data": "someData3"}] };

console.log(myObject.myArray.findIndex(i => i.hashKey === 'someHashkey2'));

If search call happens most of the time then you can use this hash to check or access your values:

Old Fashioned way

var myObject = {"myArray": [{"hashKey": "someHashkey1", "data": "someData1"}, {"hashKey": "someHashkey2", "data": "someData2"}, {"hashKey": "someHashkey3", "data": "someData3"}] };


hash = Object.create(null);
myObject.myArray.forEach(i => hash[i.hashKey] = i)

console.log('someHashkey2' in hash);
console.log(hash.someHashkey2);

New style

var myObject = {"myArray": [{"hashKey": "someHashkey1", "data": "someData1"}, {"hashKey": "someHashkey2", "data": "someData2"}, {"hashKey": "someHashkey3", "data": "someData3"}] };

var map = new Map;
myObject.myArray.forEach(i => map.set(i.hashKey, i));

console.log(map.has('someHashkey2'));
console.log(map.get('someHashkey2'));

Benefit of using map is that it allows you store anything as key and not just string

Upvotes: 1

Jared Smith
Jared Smith

Reputation: 21946

Whenever you want to get one (non-boolean) value out of an array, you almost always want reduce:

var index = myObject.myArray.reduce((index, record, i) => {
  if (index !== -1 || !('somekey' in record)) {
    return index;
  } else {
    return i;
  }
}, -1);

Or 'golfed':

var index = myObject.myArray.reduce((i, x, j) => {i || x.somekey ? i : j},-1);

Upvotes: 0

Related Questions