0

Consider following arrays:

array1 = ['a','b'];
array2 = ['a','b','c','d'];

I need to extract the difference. So my resulting array should look something like,

array3 = ['c','d'];

If an element is present in array1 then it should be poped from array2. I am looking for solutions more angular way,is there any directive available?

LS2
  • 152
  • 3
  • 16
  • There is a plain JavaScript answer available here: http://stackoverflow.com/questions/7669555/javascript-remove-array-from-array: `var c = array2.filter(function(item) { return array1.indexOf(item) === -1; });` – Carsten Massmann Jun 15 '15 at 15:54
  • possible duplicate of [JavaScript array difference](http://stackoverflow.com/questions/1187518/javascript-array-difference) – Nikhil Aggarwal Jun 15 '15 at 15:58

1 Answers1

1

In my opinion you can use underscore or lodash library for such tasks. for example in underscore you can done it through this simple code :

difference_.difference(array, *others)

Similar to without, but returns the values from array that are not present in the other arrays.

_.difference([1, 2, 3, 4, 5], [5, 2, 10]); => [1, 3, 4]

underscore annotated source

Himen
  • 1,440
  • 12
  • 18