user2879220
user2879220

Reputation: 347

How to remove array elements in javascript?

I have two arrays like this,

arr1 = ['20', 'annL', 'annT', '25', 'annL', 'annT', '44', 'annL', 'annT']

arr2 = ['2013-11-29','50','annL','annT', '20','annL','annT', '25','annL','annT', '44','annL','annT', '96','annL','annT', '26','annL','annT', '10','annL','annT']

I want to remove arr1 elements from arr2.

I have used,

array.splice() 

but couldn't able to solve the problem

Please help me,

Thank You.

Upvotes: 0

Views: 114

Answers (4)

kabomi
kabomi

Reputation: 526

Here is another solution:

arr1.forEach(
     function(item){ 
         var itemIndex = arr2.indexOf(item);
         if(itemIndex>=0)
           arr2.splice(itemIndex,1);
     }
);

About your commentary

If arr2 ist a mixed array which value is:

var arr2 = [['2013-11-29','50','annL','annT'], ['20','annL','annT', '25','annL'],
'annT', '44','annL','annT', '96','annL','annT', '26','annL','annT', '10','annL','annT'];

Passing the algorithm arr2 value will be:

[['2013-11-29','50','annL','annT'], ['20','annL','annT', '25','annL'],
 "96", "26", "annT", "10", "annL", "annT"]

If what you want is to remove the occurrences of arr1 elements in each arr2 2d array value:

var arr2 = [['2013-11-29','50','annL','annT'], ['20','annL','annT', '25','annL'],
['annT', '44','annL','annT', '96','annL','annT'], ['26','annL','annT', '10','annL','annT']];

arr2.forEach(
    function(arrayItem){ 
        arr1.forEach(function(item){ 
            if(arrayItem.indexOf(item)>=0)
                arrayItem.splice(arrayItem.indexOf(item),1);
        });
    });

//arr2 will produce: [['2013-11-29','50'], [], ['96'], ['26','10']]

Beware it will produce an error if arr2 isn't a truly 2d array.

Upvotes: 0

Niall Paterson
Niall Paterson

Reputation: 3580

Use a Filter

Array.prototype.difference = function(arr) {
    return this.filter(function(i) {return (arr.indexOf(i) == -1);});
}

Usage

arr2.difference(arr1);

[1,2,3,4].difference([1,2])
// => [3,4] 

Sidenote

I think it looks pretty in coffee script aswell:

 Array::difference = (arr) ->
   @filter (i) ->
     arr.indexOf(i) is -1

Upvotes: 3

Mr. Polywhirl
Mr. Polywhirl

Reputation: 48600

The following difference function if from this library: array.sets.js

This library provides 6 of the basic set operations.

Array.prototype.difference = function(a) {
        var temp = [];
        var keys = {};
        for (var i = 0; i < this.length; i++) {
                keys[this[i]] = 1;
        }
        for (var i = 0; i < a.length; i++) {
                if (keys[a[i]] != undefined) {
                        keys[a[i]] = 2;
                }
        }
        for (var key in keys) {
                if (keys[key] == 1) {
                        temp.push(key);
                }
        }
        return temp;
};

arr1 = ['20', 'annL', 'annT', '25', 'annL', 'annT', '44', 'annL', 'annT'];

arr2 = ['2013-11-29','50','annL','annT', '20','annL','annT', '25','annL','annT', '44','annL','annT', '96','annL','annT', '26','annL','annT', '10','annL','annT'];

console.log(arr2.difference(arr1));

Output:

["2013-11-29", "50", "96", "26", "10"]

Upvotes: 0

matewka
matewka

Reputation: 10148

Does this help?

for (var i in arr1) {
    var elemPos = arr2.indexOf(arr1[i]);
    while (-1 !== elemPos) {
        arr2.splice(elemPos, 1);
        elemPos = arr2.indexOf(arr1[i]);
    }
}

Upvotes: 0

Related Questions