The lodash method uniqWith exported as a Node.js module for creating duplicate-free arrays using custom comparator functions.
npx @tessl/cli install tessl/npm-lodash--uniqwith@4.5.0The lodash method uniqWith exported as a Node.js module. Creates a duplicate-free version of an array using a custom comparator function to determine element equality, making it particularly useful for removing duplicates from arrays of objects or complex data structures.
npm install lodash.uniqwithconst uniqWith = require('lodash.uniqwith');For ES modules (when available):
import uniqWith from 'lodash.uniqwith';Alternative import from main lodash library:
const { uniqWith } = require('lodash');
// or
import { uniqWith } from 'lodash';const uniqWith = require('lodash.uniqwith');
const isEqual = require('lodash.isequal');
// Remove duplicates from array of objects
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 }]
// Custom comparator function
const users = [
{ id: 1, name: 'Alice' },
{ id: 2, name: 'Bob' },
{ id: 1, name: 'Alice' }
];
const uniqueUsers = uniqWith(users, (a, b) => a.id === b.id);
console.log(uniqueUsers);
// => [{ id: 1, name: 'Alice' }, { id: 2, name: 'Bob' }]Creates a duplicate-free version of an array using a comparator function to determine element equality.
/**
* This method is like `_.uniq` except that it accepts `comparator` which
* is invoked to compare elements of `array`. The comparator is invoked with
* two arguments: (arrVal, othVal).
*
* @static
* @memberOf _
* @category Array
* @param {Array} array - The array to inspect
* @param {Function} [comparator] - The comparator invoked per element
* @returns {Array} Returns the new duplicate free array
*/
function uniqWith(array, comparator)Parameters:
array (Array): The array to inspect and remove duplicates fromcomparator (Function, optional): The comparator function invoked to compare elements. The comparator is invoked with two arguments: (arrVal, othVal)Returns:
Behavior:
[] if the input array is falsy (null, undefined, false, 0, "") or empty(arrVal, othVal) where arrVal is the current array element and othVal is the element being compared against_.uniq for simple equality instead)Usage Examples:
// Basic object comparison with lodash isEqual
const objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
const result = uniqWith(objects, isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]
// Custom comparison logic
const items = [
{ name: 'apple', category: 'fruit' },
{ name: 'carrot', category: 'vegetable' },
{ name: 'banana', category: 'fruit' }
];
// Remove duplicates by category
const uniqueByCategory = uniqWith(items, (a, b) => a.category === b.category);
// => [{ name: 'apple', category: 'fruit' }, { name: 'carrot', category: 'vegetable' }]
// Case-insensitive string comparison
const names = ['Alice', 'bob', 'ALICE', 'Charlie'];
const uniqueNames = uniqWith(names, (a, b) => a.toLowerCase() === b.toLowerCase());
// => ['Alice', 'bob', 'Charlie']
// Handle edge cases
uniqWith([], (a, b) => a === b); // => []
uniqWith(null, (a, b) => a === b); // => []
uniqWith(undefined, (a, b) => a === b); // => []baseUniq functionThe function is designed to handle edge cases gracefully:
[] for null, undefined, or other falsy values[] for empty input arraysThis function is part of the lodash unique array methods family:
lodash.uniq: Basic uniqueness without comparatorlodash.uniqby: Uniqueness with iteratee function for property-based deduplicationlodash.sorteduniq: Optimized for sorted arrayslodash.sorteduniqby: Sorted arrays with iteratee function