Search code examples
javascriptcoffeescriptuniquelodash

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 (look at the right), it's returning two elements with the same values for their focusIndexes. I'd expect this to return one of the two, not both. Is it because _.unique only works on primitives and not objects?

Click to expand: enter image description here


Solution

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

    _.uniqWith(clickables, _.isEqual)

    It is suggested in the docs