- Underscore.JS Tutorial
- Underscore.JS - Home
- Underscore.JS - Overview
- Underscore.JS - Environment Setup
- Underscore.JS - Iterating Collection
- Underscore.JS - Processing Collection
- Underscore.JS - Iterating Array
- Underscore.JS - Processing Array
- Underscore.JS - Functions
- Underscore.JS - Mapping Objects
- Underscore.JS - Updating Objects
- Underscore.JS - Comparing Objects
- Underscore.JS - Utilities
- Underscore.JS - Chaining
- Underscore.JS Useful Resources
- Underscore.JS - Quick Guide
- Underscore.JS - Useful Resources
- Underscore.JS - Discussion
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