Lodash remove duplicates from array

JavascriptLodash

Javascript Problem Overview


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.

Javascript Solutions


Solution 1 - 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');

Solution 2 - Javascript

You could use lodash method _.uniqWith, it is available in the current version of lodash 4.17.2.

Example:

var objects = [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }, { 'x': 1, 'y': 2 }];
 
_.uniqWith(objects, _.isEqual);
// => [{ 'x': 1, 'y': 2 }, { 'x': 2, 'y': 1 }]

More info: https://lodash.com/docs/#uniqWith

Solution 3 - Javascript

With lodash version 4+, you would remove duplicate objects either by specific property or by the entire object like so:

var users = [
  {id:1,name:'ted'},
  {id:1,name:'ted'},
  {id:1,name:'bob'},
  {id:3,name:'sara'}
];
var uniqueUsersByID = _.uniqBy(users,'id'); //removed if had duplicate id
var uniqueUsers = _.uniqWith(users, _.isEqual);//removed complete duplicates

Source:https://www.codegrepper.com/?search_term=Lodash+remove+duplicates+from+array

Solution 4 - Javascript

Or simply Use union, for simple array.

_.union([1,2,3,3], [3,5])

// [1,2,3,5]

Solution 5 - Javascript

For a simple array, you have the union approach, but you can also use :

_.uniq([2, 1, 2]);

Solution 6 - Javascript

Simply use _.uniqBy(). It creates duplicate-free version of an array.

This is a new way and available from 4.0.0 version.

_.uniqBy(data, 'id');

or

_.uniqBy(data, obj => obj.id);

Solution 7 - Javascript

You can also use unionBy for 4.0.0 and later, as follows: let uniques = _.unionBy(data, 'id')

Solution 8 - Javascript

In LODASH versions lower than 4 you will find most of this function are not implemented same way. And opposite from version 4 _.uniq was modified. I personally had a project that was in transition for a few months (from V3 -> to V4).

If you run in same situation and you have a lot of functions to be updated. You can do it in stages and when you are done with transition you can come and fix it later. This is the trick i used to avoid downtime of the platform:

/* LODASH Version 3 & 4 Compatibility Mode */
if ((_.VERSION).charAt(0) <= 3){ //Detect LODASH version 3 or 4.
    //V3 or lower
    _.uniq(data, true, 'id');
} else {
    //V4 or Higher
	_.uniqBy(data, 'id');
}

> Also if you look at lodash documentation for most of this cases you > can find migration of _.uniq from version lower than 4 can be performed > with both functions: > > _.uniqBy(data, 'id') or > _.unionBy(data, 'id') > > Both of them will bring same result. I personally was guessing which > one to pick. In the end I picked this one: _.uniqBy(data, 'id').

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChrisRichView Question on Stackoverflow
Solution 1 - JavascriptntalbsView Answer on Stackoverflow
Solution 2 - JavascriptzdrsoftView Answer on Stackoverflow
Solution 3 - JavascriptTaylor HawkesView Answer on Stackoverflow
Solution 4 - JavascriptVixsonView Answer on Stackoverflow
Solution 5 - JavascriptZettaPView Answer on Stackoverflow
Solution 6 - JavascriptHassan AjazView Answer on Stackoverflow
Solution 7 - JavascriptHimanshu TanwarView Answer on Stackoverflow
Solution 8 - JavascriptDmitryView Answer on Stackoverflow