You can remove one array from another in javascript or jquery

I have three arrays in javascript

var array1 = new Array (1,2,3,4,5); var array2 = new Array ("a", "b", "c", "d", "e"); var array3 = new Array ("a", "c", "d"); 

and I basically want:

  • Create a new array with array2 elements minus in the array. Therefore, this should lead to

    var array4 = new Array {"b", "e"};

  • Create another array with the corresponding index array1, which aligns with array 4, so in this case I also want to be able to generate

    var array5 = new Array {2, 5}

I know that in dotnet 3.5 there are many simple methods for this operation, but was not sure if javascript had something like that.

+3
javascript jquery arrays
Jan 15 '10 at 2:37
source share
9 answers

Well, for starters, declaring an array takes one of two forms:

 var myArr = new Array(1, 2, 3); // or var myArr = [1, 2, 3]; 

The second option is better, by the way. See here for reasons .

What you are really looking for is called the difference (as opposed to intersection or union). There are some more questions about the stack overflow about the difference of arrays, like this one , however the decision made uses for .. in for the array, which is not very good. I heard that Doug Crockford literally hits you in the face via the Internet every time you do it. Here's a more technical discussion about this if you're interested.

The answer to this question may suit you better.

+9
Jan 15
source share

This is greatly simplified with the Javascript 1.6 Array functions:

 Array.prototype.remove = function(set){return this.filter( function(e,i,a){return set.indexOf(e)<0} )}; Array.prototype.mapTo = function(set,to){return this.map( function(e,i,a){return to[set.indexOf(e)]} )}; var array1 = [1,2,3,4,5]; var array2 = ["a", "b", "c", "d", "e"]; var array3 = ["a", "c", "d"]; var array4 = array2.remove(array3); var array5 = array4.mapTo(array2, array1); 
+6
Jan 15
source share

Using jQuery:

 var array4 = $.grep(array2, function(n, i){ return $.inArray(n, array3) == -1; }); 

For pure JavaScript, see the following questions:

  • Javascript Array Difference
  • What is the fastest or most elegant way to calculate the difference in sets using Javascript arrays?
+4
Jan 15 '10 at 2:55
source share

A very short and effective way: if you use:

 _.difference(array1,array2) 
+4
Feb 27 '14 at 13:55
source share

I recommend following these steps: http://www.dustindiaz.com/sugar-arrays/

Makes working with arrays a lot easier. Then the solution to your problem will look like this:

 var array1 = [1, 2, 3, 4, 5], array2 = ["a", "b", "c", "d", "e"], array3 = ["a", "c", "d"], array5 = [], array4 = array2.filter(function(item, i) { var ok = array3.indexOf(item) === -1; if (ok) { array5.push(i + 1); } return ok; }); alert(array4); alert(array5); 
+1
Jan 15 '10 at 7:45
source share

This solution only works if array2 and array3 have only strings or numbers.

Step 1. Convert array3 into an object with properties (for faster searches)

 var obj = {}, i; for(i=0;i<array3.length;i++) { obj[array3[i]] = true; } 

Step 2: loop through array2 and get only elements not in array 3

 var array4 = [], array5 = []; for(i=0; i<array2.length;i++) { if (!obj.hasOwnProperty(array2[i])) { array4.push(array2[i]); array5.push(array1[i]); } } 
0
Jan 15 '10 at 2:55
source share

javascript is a compact design language.

if you don’t have a library and want a convenient way to work in most browsers, you need to write code.

 Array.prototype.lastIndex= function(what){ var L= this.length; while(L){ if(this[--L]=== what) return L; } return -1; } Array.prototype.remove= function(arg){ var what, L= arg.length, ax; while(L && this.length){ what= arg[--L]; while((ax= this.lastIndex(what))!= -1){ this.splice(ax, 1); } } return this; } var array2 = ["a", "b", "c", "d", "e"], array3 = ["a", "c", "d"]; alert(array2.remove(array3)) 
0
Jan 15
source share

Underscore.js includes difference methods and maps to simplify this:

 var array4 = _.difference(array2, array3); var array5 = _.map(array4, function(i){ return array1[array2.indexOf(i)] }); 
0
Apr 08 2018-12-12T00:
source share

It really helped me:

 var a = [1, 2, 3, 4]; var b = [2, 3, 4, 5]; var c = _.without.apply(_, [a].concat(b)); 
  Result: c = [1] 
0
Apr 04 '13 at 11:18
source share



All Articles