Filter Duplicates with Lodash’s uniq() Function

How to Filter an Object by Key in JavaScript

Apr 7, 2022

To filter duplicates from an array, use Lodash’s uniq() functio.
This function will remove any duplicate values from the provided array.

const _ = require('lodash');
const array = [1, 2, 3, 4, 5, 5, 6, 7, 7];


The _.uniq() function compares values using SameValueZero comparison.
SameValueZero works well for primitive values, but not for objects.

The uniqBy() function is similar to the uniq() function, with the key difference that it allows you to pass in a function that returns the value you want to compare by.
For example, below is how you filter duplicate objects by name property.

const array = [
  { name: 'Badger' },
  { name: 'Badger' },
  { name: 'Badger' },
  { name: 'Mushroom' },
  { name: 'Mushroom' }
_.uniqBy(array, obj =>; 


The uniqWith() function takes a comparator function, which should return true if the two values should be considered equal.
For example, below is how you filter out duplicate objects in an array using Lodash’s isEqual() function

const array = [
  { x: 1, y: 2 },
  { x: 2, y: 1 },
  { x: 1, y: 2 }
_.uniqWith(array, _.isEqual); 

More Lodash Tutorials

Source link

Leave a reply

Please enter your comment!
Please enter your name here