Underscore.JS - uniq method



Syntax

_.uniq(array, [isSorted], [iteratee])

uniq method returns a array of unique values from an array. It uses type safe equaliy(===) check. If array is sorted, pass true to run a faster algorithm. A iteratee function can be passed to determine the uniqueness of items.

Example

var _ = require('underscore');

var list1 = [11, 2, 14, 14, 2, 6]
var list2 = [1, 2, 3, 3, 3, 4, 5]
//Example 1: uniq values
result = _.uniq(list1);
console.log(result)

//Example 2: uniq values of an sorted array
result = _.uniq(list2, true);
console.log(result)

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

Command

\>node tester.js

Output

[ 11, 2, 14, 6 ]
[ 1, 2, 3, 4, 5 ]
underscorejs_processing_array.htm
Advertisements