The unionBy
function from lodash is used to merge multiple arrays and return a new array with unique values, based on a specific property. The syntax for using unionBy is:
index.tsx43 chars2 lines
Here, [arrays]
is an array of arrays that needs to be merged, and [iteratee=_.identity]
is an optional parameter which takes a function that is called for each element in the arrays to generate the value to be used for uniqueness comparison.
Let's say we have two arrays with objects having a productId
property, and we want to merge them to get a new array with unique productId
:
index.tsx389 chars10 lines
Here, the third parameter 'productId'
is used to identify unique elements during the merge. The resulting array will contain only one instance of each productId
.
So, this is how you can use the unionBy
function from lodash to merge arrays and return a new array with unique values based on a specific property.
gistlibby LogSnag