I have two arrays
var array1 = new Array ["a", "b", "c", "d", "e"];
var array2 = new Array ["a", "c", "d"];
I want to remove elements of array2 from array1
Result ["b", "e"]
Is there anything like
array1 = array1.remove(array2)
Note I'm using jquery-1.9.1
I have two arrays
var array1 = new Array ["a", "b", "c", "d", "e"];
var array2 = new Array ["a", "c", "d"];
I want to remove elements of array2 from array1
Result ["b", "e"]
Is there anything like
array1 = array1.remove(array2)
Note I'm using jquery-1.9.1
Share Improve this question asked Sep 25, 2013 at 7:28 OkkyOkky 10.5k15 gold badges77 silver badges123 bronze badges 1- possible duplicate of JavaScript array difference – Itay Commented Sep 25, 2013 at 7:31
5 Answers
Reset to default 8Try:
var diff = $(array1).not(array2).get();
function difference(source, toRemove) {
return source.filter(function(value){
return toRemove.indexOf(value) == -1;
});
}
NOTE: Array.prototype.indexOf
and Array.prototype.filter
are not available before IE9!
Although lot of ways to achieve it through native java script but i remend to see Underscore library
Underscore JS is what you need. This library has lots of useful array manipulation functions. Underscore JS
Underscore.js library helps: Hers is what you need
_.difference(array1, array2);