Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lodash unique based on attribute

I have a array of objects such as

[{ "name": "Jen", "age":31, "eyecolor": "blue", "hair":"brown"}, 
 { "name": "Jen", "age":32, "eyecolor": "green", "hair":"blonde"}, 
 { "name": "Jules", "age":31, "eyecolor": "blue", "hair":"brown"}, ,
 { "name": "Brian", "age":40, "eyecolor": "blue", "hair":"brown"}]

I need a lodash expression that will result in a array of unique objects based on one property. For example, if I used name, it would give me these three:

[{ "name": "Jen", "age":31, "eyecolor": "blue", "hair":"brown"}, 
 { "name": "Jules", "age":31, "eyecolor": "blue", "hair":"brown"}, 
 { "name": "Brian", "age":40, "eyecolor": "blue", "hair":"brown"}]
like image 883
sarsnake Avatar asked Nov 18 '15 20:11

sarsnake


People also ask

What is _ get?

The _. get() function is an inbuilt function in the Underscore. js library of JavaScript which is used to get the value at the path of object. If the resolved value is undefined, the defaultValue is returned in its place. Syntax: _.get(object, path, [defaultValue])

What is Noop Lodash?

The Lodash _. noop() method is used to return “undefined” irrespective of the arguments passed to it. Syntax: _.noop() Parameters: This method can take optional parameters of any type. Returns: This method returns undefined.

How do I compare two arrays in Lodash?

isEqual() Method. The Lodash _. isEqual() Method performs a deep comparison between two values to determine if they are equivalent. This method supports comparing arrays, array buffers, boolean, date objects, maps, numbers, objects, regex, sets, strings, symbols, and typed arrays.


2 Answers

For Lodash versions 4+ use _.uniqBy():

var arr = [{"name":"Jen","age":31,"eyecolor":"blue","hair":"brown"},{"name":"Jen","age":32,"eyecolor":"green","hair":"blonde"},{"name":"Jules","age":31,"eyecolor":"blue","hair":"brown"},{"name":"Brian","age":40,"eyecolor":"blue","hair":"brown"}];

var unique = _.uniqBy(arr, 'name');

console.log(unique);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.js"></script>

For Lodash versions under 4 use _.uniq():

var arr = [{"name":"Jen","age":31,"eyecolor":"blue","hair":"brown"},{"name":"Jen","age":32,"eyecolor":"green","hair":"blonde"},{"name":"Jules","age":31,"eyecolor":"blue","hair":"brown"},{"name":"Brian","age":40,"eyecolor":"blue","hair":"brown"}];

var unique = _.uniq(arr, 'name');

console.log(unique);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.js"></script>
like image 187
Ori Drori Avatar answered Sep 24 '22 06:09

Ori Drori


If your lodash version is higher than 4.0, You should use _.uniqBy().

At this time, _.uniq() does not work, it only accept one parameter, which cannot address iterated objects.

var arr = [{ "name": "Jen", "age":31, "eyecolor": "blue", "hair":"brown"}, 
 { "name": "Jen", "age":32, "eyecolor": "green", "hair":"blonde"}, 
 { "name": "Jules", "age":31, "eyecolor": "blue", "hair":"brown"},
 { "name": "Brian", "age":40, "eyecolor": "blue", "hair":"brown"}];

var result = _.uniqBy(arr, 'name');
console.log(result)
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Reference
https://lodash.com/docs/4.17.4#uniqBy

like image 26
Yu Huang Avatar answered Sep 26 '22 06:09

Yu Huang