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 focusIndex
es. 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:
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 focusIndex
es. 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:
Share Improve this question edited Jul 6, 2014 at 3:28 Daniel Kaplan asked Jul 5, 2014 at 23:00 Daniel KaplanDaniel Kaplan 67.4k57 gold badges268 silver badges399 bronze badges3 Answers
Reset to default 9_.uniqWith
is what you might need so that you can do parison using _.isEqual
_.uniqWith(clickables, _.isEqual)
It is suggested in the docs
It doesn't work because paring 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 parison 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.
apparently for lodash version 4.x.x _.uniq works with primitives, If you want to work with array of json's the use _.uniqBy
uniqueByFocusIndex = _.uniqBy(clickables,"focusIndex");