views:

256

answers:

6

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.

+2  A: 

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.

nickf
A: 

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]);
    }
}
Chetan Sastry
A: 

Using jQuery:

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

For pure JavaScript, see these questions:

Alex Reisner
+2  A: 

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);
Ajw
A: 

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))
kennebec
A: 

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);
einarq