Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Lodash remove duplicates from array

This is my data:

[     {         url: 'www.example.com/hello',         id: "22"         },     {         url: 'www.example.com/hello',         id: "22"         },     {         url: 'www.example.com/hello-how-are-you',         id: "23"         },     {         url: 'www.example.com/i-like-cats',         id: "24"         },     {         url: 'www.example.com/i-like-pie',         id: "25"         } ] 

With Lodash, how could I remove objects with duplicate id keys? Something with filter, map and unique, but not quite sure.

My real data set is much larger and has more keys, but the concept should be the same.

like image 673
ChrisRich Avatar asked Jul 31 '15 07:07

ChrisRich


People also ask

How to remove duplicates in an array Lodash?

We can remove duplicate elements from an array using the _. uniq() method of Lodash. This method keeps the first instance of an element and removes the remaining one. Therefore, the order of an element in the resultant array depends on its occurrence in the array.

How do you remove duplicates from an array?

We can remove duplicate element in an array by 2 ways: using temporary array or using separate index. To remove the duplicate element from array, the array must be in sorted order. If array is not sorted, you can sort it by calling Arrays. sort(arr) method.

Does Lodash Uniq work on objects?

Lodash's 'uniq' family of methods can be implemented in plain JS without trouble. Lodash is a very useful utility library that lets us work with objects and arrays easily.

How do you find duplicate values in an array of objects using Lodash?

To get duplicate values from an array with Lodash, we can use the countBy method to count the values. Then we call the JavaScript array's reduce method to get all the items that has count more than 1 and put them in an array.

How do you get rid of duplicates in an array?

1) Remove duplicates from an array using a Set A Setis a collection of unique values. To remove duplicates from an array: First, convert an array of duplicates to a Set. The new Setwill implicitly remove duplicate elements.

How to Uniq with an array of objects in Lodash?

Assuming that the data should be uniqued by each object's id property and your data is stored in data variable, you can use the _.unique () function like this: You could use lodash method _.uniqWith, it is available in the current version of lodash 4.17.2. Or simply Use union, for simple array.

How to remove duplicates from a list in JavaScript?

To remove the duplicates, you use the filter () method to include only elements whose indexes match their indexOf values: let chars = [ 'A', 'B', 'A', 'C', 'B' ]; let uniqueChars = chars.filter ((c, index) => { return chars.indexOf (c) === index; }); console.log (uniqueChars); Code language: JavaScript (javascript)

How to find duplicate values in a list of letchars?

To find the duplicate values, you just need to reverse the condition: letchars = ['A', 'B', 'A', 'C', 'B']; letdupChars = chars.filter((c, index) =>{ returnchars.indexOf(c) !== index; }); console.log(dupChars); Code language:JavaScript(javascript)


Video Answer


1 Answers

_.unique no longer works for the current version of Lodash as version 4.0.0 has this breaking change. The functionality of _.unique was splitted into _.uniq, _.sortedUniq, _.sortedUniqBy, and _.uniqBy.

You could use _.uniqBy like this:

_.uniqBy(data, function (e) {   return e.id; }); 

...or like this:

_.uniqBy(data, 'id'); 

Documentation: https://lodash.com/docs#uniqBy


For older versions of Lodash (< 4.0.0 ):

Assuming that the data should be uniqued by each object's id property and your data is stored in data variable, you can use the _.unique() function like this:

_.unique(data, function (e) {   return e.id; }); 

Or simply like this:

_.uniq(data, 'id'); 
like image 149
ntalbs Avatar answered Sep 17 '22 13:09

ntalbs