Combining _.uniq execution with _.isEqual in lodash

lodash provides a method _.uniq()for finding unique elements from an array, but the comparison function used is strict equality ===, while I want to use _.isEqual()one that satisfies:

_.isEqual([1, 2], [1, 2])
// true

Is there a way to accomplish _.uniq()using _.isEqual()without writing my own method?

+4
source share
2 answers

According to lodash v4 exists _.uniqWith(array, _.isEqual). from documents:

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 }]

Additional information: https://lodash.com/docs#uniqWith

+4
source

isEqual() uniq(), , , . (/) :

_.reduce(coll, function(results, item) {
    return _.any(results, function(result) {
        return _.isEqual(result, item);
    }) ? results : results.concat([ item ]);
}, []);

reduce() . any() isEqual(), , . , .

+2

All Articles