To use the difference
function from the Underscore library in TypeScript, first, we need to install the Underscore library using npm:
index.ts23 chars2 lines
Then, we can import the library in our TypeScript file as follows:
index.ts33 chars2 lines
Now, we can use the _.difference
method to find the difference between two arrays. The method takes two or more arguments, where the first array is the source array and the remaining arguments are the arrays you want to find the difference of. Here's an example:
index.ts163 chars5 lines
In the above example, we imported the underscore library, defined an array called originalArray
with the initial values, and found the difference between originalArray
and another array [2, 4, 6]
using the _.difference
method. The result, [1, 3, 5]
, is assigned to diffArray
, which is then logged to the console.
Note that the difference
function returns a new array with unique values, so if the original array contains duplicate values that are not in the compared arrays, they will be kept in the result.
gistlibby LogSnag