leora
leora

Reputation: 196689

can you remove one array from another in javascript or jquery

i have a 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 to:

  1. Create a new array with array2 minus items in array3. So it should result in

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

  2. Create another array with the corresponding index of array1 that aligns with array 4, so in this case i would also want to be able to generate

    var array5 = new Array {2, 5}

i know in dotnet 3.5 there are lots of simple methods to do this operation but wasn't sure if javascript had anything similar.

Upvotes: 3

Views: 13939

Answers (9)

anurag_29
anurag_29

Reputation: 932

A very short and efficient way would be if you use :

_.difference(array1,array2)

Upvotes: 3

Ikrom
Ikrom

Reputation: 5123

This 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]

Upvotes: 0

Dave James Miller
Dave James Miller

Reputation: 5288

Underscore.js includes difference & map methods to simplify to this:

var array4 = _.difference(array2, array3);
var array5 = _.map(array4, function(i){ return array1[array2.indexOf(i)] });

Upvotes: 0

einarq
einarq

Reputation: 535

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

Makes working with arrays a whole lot easier. The solution to your problem would then 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);

Upvotes: 1

kennebec
kennebec

Reputation: 104810

javascript is a compact language, by design.

if you don't have a library, and want a convenient method to work in most browsers, you need to write some 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))

Upvotes: 0

Ajw
Ajw

Reputation: 716

This is made much simpler using 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);

Upvotes: 6

Alex Reisner
Alex Reisner

Reputation: 29447

Using jQuery:

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

For pure JavaScript, see these questions:

Upvotes: 4

Chetan S
Chetan S

Reputation: 23813

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

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

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

step 2: loop thru the array2 and get only the elements not in array3

var array4 = [], array5 = [];
for(i=0; i<array2.length;i++) {
    if (!obj.hasOwnProperty(array2[i])) {
        array4.push(array2[i]);
        array5.push(array1[i]);
    }
}

Upvotes: 0

nickf
nickf

Reputation: 546273

Ok, for starters, array declaration takes one of two forms:

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

The second option is better, btw. See here for reasons.

What you're actually looking for is something called difference (as opposed to intersection or union). There's some other questions on Stack Overflow about array difference, like this one, however the accepted solution uses for .. in on an array, which is not a good idea. I heard Doug Crockford literally punches you in the face through the Internet every time you do that. Here's a more technical discussion about it if you're interested.

The answers to this question might suit you better.

Upvotes: 11

Related Questions