0

I have the two following arrays:

array1 = [{'itemCode':'a'}, {'itemCode':'b'}, {'itemCode':'c'}]; 
array2 = [{'itemCode':'a'}, {'itemCode':'b'}, {'itemCode':'d'}]; 

I'm looking for a way to slice array2 from array 1 and return the "F" value.

Does anyone know of a quick way to do this in JavaScript, Im totally stumped.

I tried _difference and _unique from underscore, but that doesnt seem to be the right solution.

var diffArray = _.difference(array1, array2);

Any help would be greatly appreciated!

Jake Rutter
  • 1
  • 1
  • 1

1 Answers1

0

Get the unique values from two arrays and put them in another array - Jquery I think this will answer your question

var unique = [];
for(var i = 0; i < array1.length; i++){
    var found = false;
    for(var j = 0; array2.length; j++){
     if(array1[i].itemCode == array2[j].itemCode){
      found = true;
      break; 
    }
   }
   if(found == false){
   unique.push(array1[i]);
  }
}
Community
  • 1
  • 1
ADB
  • 3
  • 2
  • I don't think `indexOf()` will work when the elements are objects like this. It doesn't compare objects recursively. – Barmar May 08 '15 at 21:03
  • Yes you are right, i overlooked the datatype. But it sort of demonstrate how its done. I am not familiar with javascript – ADB May 08 '15 at 21:15
  • It doesn't really demonstrate how it's done, because you have to do something completely different when `indexOf()` can't match them. – Barmar May 08 '15 at 21:17