how to use the unique function from the underscore library in javascript

To use the unique function from the underscore library in JavaScript, first make sure you have installed underscore.js. Then, you can call the unique function by passing an array as a parameter:

index.tsx
var _ = require('underscore');

var arr = [1, 2, 2, 3, 4, 4, 5];
var uniqueArr = _.uniq(arr);

console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
146 chars
7 lines

In the above example, we first require the underscore library using var _ = require('underscore');. Then, we define an array arr with duplicate values. Finally, we use the _.uniq() function to get an array with unique values and store the result in uniqueArr.

Note that _.uniq() function does not modify the original array, but returns a new array with unique values.

gistlibby LogSnag