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"}]
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])
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.
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.
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>
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
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