DEV Community

Cover image for Remove Duplicate from Array and Object from Array
RetiFier
RetiFier

Posted on

Remove Duplicate from Array and Object from Array

From Array

_.uniq(array) From Lodash

Creates a duplicate-free version of an array, using SameValueZero for equality comparisons, in which only the first occurrence of each element is kept.

Example

const arr = [1 , 1 , 2 , 3]
const removeDuplicate = _.uniq(arr);
console.log(removeDuplicate)
// Result => [1,2,3]

Object From Array

_.uniqBy[array, objectName] From Lodash

This method is like _.uniq except that it accepts iteratee which is invoked for each element in array to generate the criterion by which uniqueness is computed.

Example

const arr = [{id: 1 , name: 'Dota'}, {id:1 , name: 'Dota'} , {id:2 , name:'LOL'}]
const removeDuplicate = _.uniqBy(arr, 'id');
console.log(removeDuplicate)
// Result => [{id: 1 , name: 'Dota'} , {id:2 , name:'LOL'}]

Top comments (0)