Underscore.JS - union method



Syntax

_.union(*arrays)

union method returns a union of passed arrays i.e. a combined array of unique 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: union of list1 and list2
result = _.union(list1, list2);
console.log(result)

//Example 2: union of list1, list2, list3
result = _.union(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, 4,
  5, 6, 7
]
[
  1, 2, 3, 4,
  5, 6, 7, 8
]
underscorejs_processing_array.htm
Advertisements