how to use the uniq function from the lodash library in typescript

To use the uniq function from the lodash library in TypeScript, you first need to install the library. You can do this using npm by running the following command:

npm install --save lodash
26 chars
2 lines

Once you have installed the library, you can import the uniq function in your TypeScript code as follows:

index.ts
import * as _ from 'lodash';

const arr = [1, 2, 2, 3, 3, 3];
const uniqueArr = _.uniq(arr);

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

In the code above, we first import the entire lodash library using the import * as _ statement. We then use the uniq function from the library to generate a new array with unique values only.

Note that we use the const keyword to declare a new constant named uniqueArr to store the resulting array. Finally, we log the resulting array to the console using the console.log statement.

gistlibby LogSnag