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.
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.
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.
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.
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.
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.
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.
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)
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)
_.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');
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With