Underscore.JS - intersection method



Syntax

_.intersection(*arrays)

intersection method returns a intersection of passed arrays i.e. an array of common values from each array.

Example

var _ = require('underscore');

var list1 = [1, 2, 3, 4, 5, 6]
var list2 = [1, 2, 3, 7]
var list3 = [1, 2, 4, 5, 6, 7, 8]
//Example 1: intersection of list1 and list2
result = _.intersection(list1, list2);
console.log(result)

//Example 2: intersection of list1, list2, list3
result = _.intersection(list1, list2, list3);
console.log(result)

Save the above program in tester.js. Run the following command to execute this program.

Command

\>node tester.js

Output

[ 1, 2, 3 ]
[ 1, 2 ]
underscorejs_processing_array.htm
Advertisements