how to use the uniqwith function from lodash in javascript

To use the uniqWith function from lodash, follow the steps below:

  1. Import the uniqWith method from lodash:
index.tsx
const { uniqWith } = require('lodash');
40 chars
2 lines
  1. Create an array of objects or arrays that you want to compare:
index.tsx
const arr = [
  [1, 2],
  [1, 2],
  [2, 3]
];
46 chars
6 lines
  1. Define a function that will perform the comparison between the objects or arrays in arr. In this example, we will use a function to compare arrays:
index.tsx
const compareArrays = (arr1, arr2) => {
  return arr1[0] === arr2[0] && arr1[1] === arr2[1];
};
96 chars
4 lines
  1. Call the uniqWith method and pass the array and the comparison function as arguments:
index.tsx
const result = uniqWith(arr, compareArrays);
45 chars
2 lines
  1. The result variable will be an array that contains the unique objects or arrays in the original arr array based on the comparison function. In this example, the result array will contain:
index.tsx
[
  [1, 2],
  [2, 3]
]
23 chars
5 lines

gistlibby LogSnag