0

I have an array of objects, i was trying to iterate over. The array is pretty simple in format.

{a:5, b:"key", c:19}

i was trying to compare an array with a subset, say: [{a:5},...]

for (var i = 0; i < subset.length; i++) {
    var searchTerm = subset[i].a;

    var index = objs.indexOf(searchTerm, function (el) {
        return el.a;
    });

    if (index > -1) {
        objs[index].Found = true;
    }
}

So that way ultimately objs, could have a new key in it, 'Found'

This way, it will set the main array objs item.Found = true, if it existed in subset.

There are 2 issues though. Accounting for multiple instances of the same item, and the fact that this current implementation doesnt seem to work.

This is a slight expansion of (indexOf method in an object array? )but with an array of search terms.

ideally, i dont want to change the arrays at all, so im trying not to slice, etc.

In a lot of the defintions, indexOf is defined as:

function indexOf (key, start); 

instead of the ideas i am trying to work with.

Edit

Here is some code that i have to get this working, but i was thinking there is a more effecient way to do it than written.

for (var j = 0; j < compare.length; j++){
  var searchTerm = compare[j]["a"];
  for (var k = 0; k < objs.length; k++){
      if (!objs[k].Found && objs[k]["a"] == searchTerm){
           objs[k].Found = true;
          break;
      }
  } 
}
Community
  • 1
  • 1
Fallenreaper
  • 10,222
  • 12
  • 66
  • 129
  • What if more than one object matches? – thefourtheye Apr 21 '15 at 15:06
  • Since when does `indexOf` take a function as a second argument? – Adam Jenkins Apr 21 '15 at 15:10
  • 1
    @Adam It doesn't. If I understand it correctly, the question is how to do something like this. – jwatts1980 Apr 21 '15 at 15:11
  • There's a ton of methods you can use on `Arrays` that take callbacks: `map`, `some`, `every`, etc. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array – Adam Jenkins Apr 21 '15 at 15:13
  • One of the answers in the linked item, had used a defined function, I was trying to find something more effecient then what i am about to submit as an edit – Fallenreaper Apr 21 '15 at 15:14
  • @Fallenreaper - the most up-voted answer in the linked question uses `map` – Adam Jenkins Apr 21 '15 at 15:15
  • @Fallenreaper Even if there was an overload of `indexOf` that worked like this, internally it would have to iterate over the second array. With regard to efficiency, in your case there are still 2 arrays that have to be looped over... – jwatts1980 Apr 21 '15 at 15:16
  • @Adam I mustve scrolled passed that. I did not see the map option. – Fallenreaper Apr 21 '15 at 15:17
  • That looks like an object, not an array. – Scimonster Apr 21 '15 at 15:19
  • i guess i could just do something like that answer then. create 2 maps and walk those instead of walking 2 arrays of objects. Then you consume a bit more memory, though this is negligible in the cases i am running against (less than 500). With an additional loop, doing a .map().indexOf() on each iteration is just absurd – Fallenreaper Apr 21 '15 at 15:19
  • 2
    I don't think it can get any more efficient than the example in your edit. There are a lot of ways in JS to refactor this, but I don't think you're ever going to beat `array1.length * array2.length` iterations. – jwatts1980 Apr 21 '15 at 15:25
  • thats what i thought. Damn. Ok. Thanks @jwatts1980 – Fallenreaper Apr 21 '15 at 15:27

0 Answers0