What is lodash.uniqwith?
The lodash.uniqwith package is a utility library that provides a method to create a duplicate-free version of an array, using a custom comparator function to determine the uniqueness of elements. This is particularly useful when dealing with complex data structures where simple equality checks are insufficient.
Remove duplicates with custom comparator
This feature allows you to remove duplicate objects from an array using a custom comparator function. In this example, the _.isEqual function is used to compare the objects for equality.
const _ = require('lodash.uniqwith');
const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
const result = _.uniqWith(objects, _.isEqual);
console.log(result); // [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]