Skip to content Skip to sidebar Skip to footer

Why Isn't Lodash's _.unique Returning Unique Objects When I Pluck A Field That Is An Object?

I'm using lodash's _.unique and it's not working as expected. I'm doing this: uniqueByFocusIndex = _.unique(clickables, false, 'focusIndex'); And as you can see in the image (loo

Solution 1:

_.uniqWith is what you might need so that you can do comparison using _.isEqual

_.uniqWith(clickables, _.isEqual)

It is suggested in the docs

Solution 2:

It doesn't work because comparing objects is done by reference and returns false even if the objects' contents are the same.

Using a string for the callback will check those values using the pluck callback style, but comparison of those objects you have under that key will always be false.

I tried to find a way to do this with some other callback, but I think you would be better off just writing your own uniq function that fits your purposes.

Post a Comment for "Why Isn't Lodash's _.unique Returning Unique Objects When I Pluck A Field That Is An Object?"