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

To use the uniq function from the Underscore library in JavaScript, first you need to include the Underscore library in your project:

<script src="https://underscorejs.org/underscore-min.js"></script>
67 chars
2 lines

Then you can call the uniq function passing an array as an argument and it will return a copy of the array with duplicates removed:

index.tsx
const arr = [1, 2, 3, 3, 4, 5, 5];
const uniqueArr = _.uniq(arr);
console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
117 chars
4 lines

Alternatively, you can use the chain syntax provided by Underscore:

index.tsx
const arr = [1, 2, 3, 3, 4, 5, 5];
const uniqueArr = _(arr).uniq().value();
console.log(uniqueArr); // Output: [1, 2, 3, 4, 5]
127 chars
4 lines

In both cases, the output will be an array with unique values only.

gistlibby LogSnag