The unionWith
function from lodash
allows you to find the union of two arrays (i.e. the elements that are present in both arrays) using a comparator function that you specify.
The syntax for using unionWith
is as follows:
index.tsx43 chars2 lines
where:
array
(Array): The array to inspect.[values]
(Array): The values to exclude.[comparator]
(Function): The comparator function invoked per element.Here's an example of how to use unionWith
:
index.tsx274 chars9 lines
In this example, we are finding the union of array1
and array2
using the isEqual
function as the comparator. This function is used to compare each element of the arrays to determine if they are equal.
The result
variable will contain an array with the elements that are present in both array1
and array2
, using the isEqual
comparator function to determine equality.
gistlibby LogSnag