0

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.

2
  • Implement a relative complement set operation... Commented Nov 29, 2013 at 10:20
  • if you are in a hurry there are loads of great utility libraries like lodash and underscore that provide functionality like array#diff: lodash.com/docs#difference Commented Nov 29, 2013 at 10:20

4 Answers 4

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

Nice indeed. Note that unlike "hash" solutions this works with any type (i.e. arrays of objects).
Great let me know :) if there's anything that needs clarification just say
0

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]);
    }
}

1 Comment

Note: indexOf on arrays were first added in IE9+. Check this for compatibility methods.
0

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

Comments

0

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.

1 Comment

yeah, you can use it but I don't know if the results would be the appropriate. I've updated my answer with this question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.