Skip to content Skip to sidebar Skip to footer

Finding A - B From Two Arrays Using Underscore.js

I have to filter out certain elements from an array in javascript and thought of using underscore.js for this purpose. As I am new to it,some help is appreciated.Please refer the c

Solution 1:

By using difference method:

var c = _.difference(a, b);

http://documentcloud.github.com/underscore/#difference

Solution 2:

I have to find A- B and assign the result to C . Does underscore.js has any convinience method to do that ?

Yes, you can use difference method:

var c = _.difference(a, b);

Solution 3:

How about:

var a = [1, 2, 5, 6], b = [6], c;

c = a.filter(
    function (aItem) {
        return !(~b.indexOf(aItem));
    }
);

console.log(c);

​ ​

Post a Comment for "Finding A - B From Two Arrays Using Underscore.js"