Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I get a unique array based on object property using underscore

I have an array of objects and I want to get a new array from it that is unique based only on a single property, is there a simple way to achieve this?

Eg.

[ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]

Would result in 2 objects with name = bill removed once.

like image 353
el_pup_le Avatar asked Dec 23 '13 08:12

el_pup_le


People also ask

How do I find unique objects in an array?

One way to get distinct values from an array of JavaScript objects is to use the array's map method to get an array with the values of a property in each object. Then we can remove the duplicate values with the Set constructor. And then we can convert the set back to an array with the spread operator.

What is the use of IS underscore array function?

Computes the list of values that are the intersection of all the arrays. Each value in the result is present in each of the arrays. Similar to without, but returns the values from array that are not present in the other arrays. Produces a duplicate-free version of the array, using === to test object equality.

How do you access the properties of an array of objects?

A nested data structure is an array or object which refers to other arrays or objects, i.e. its values are arrays or objects. Such structures can be accessed by consecutively applying dot or bracket notation. Here is an example: const data = { code: 42, items: [{ id: 1, name: 'foo' }, { id: 2, name: 'bar' }] };

How do you change the properties of an array of objects?

To update an object's property in an array of objects, use the map() method to iterate over the array. On each iteration, check if the current object is the one to be updated. If it is, modify the object and return the result, otherwise return the object as is. Copied!


2 Answers

Use the uniq function

var destArray = _.uniq(sourceArray, function(x){
    return x.name;
});

or single-line version

var destArray = _.uniq(sourceArray, x => x.name);

From the docs:

Produces a duplicate-free version of the array, using === to test object equality. If you know in advance that the array is sorted, passing true for isSorted will run a much faster algorithm. If you want to compute unique items based on a transformation, pass an iterator function.

In the above example, the function uses the objects name in order to determine uniqueness.

like image 73
ColinE Avatar answered Oct 19 '22 18:10

ColinE


If you prefer to do things yourself without Lodash, and without getting verbose, try this uniq filter with optional uniq by property:

const uniqFilterAccordingToProp = function (prop) {
    if (prop)
        return (ele, i, arr) => arr.map(ele => ele[prop]).indexOf(ele[prop]) === i
    else
        return (ele, i, arr) => arr.indexOf(ele) === i
}

Then, use it like this:

const obj = [ { id: 1, name: 'bob' }, { id: 1, name: 'bill' }, { id: 1, name: 'bill' } ]
obj.filter(uniqFilterAccordingToProp('abc'))

Or for plain arrays, just omit the parameter, while remembering to invoke:

[1,1,2].filter(uniqFilterAccordingToProp())

like image 9
Eirik Birkeland Avatar answered Oct 19 '22 19:10

Eirik Birkeland