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

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.

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)