Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

Already on GitHub? Sign in to your account

_.uniq is unable to do his job with objects #644

Closed
ontologiae opened this Issue Jun 15, 2012 · 3 comments

Comments

Projects
None yet
2 participants

var a = _.uniq([{u:"k"},{u:"k"}]);

JSON.stringify(a);

gives :
"[{"u":"k"},{"u":"k"}]"

Reason ? Underscore uses === and not _.isEqual to test equality of objects.

I know it's slower, but in the real world we don't use map, fold, filter and all theses good functions you provide in your lib to use it with integer, string or char !

Maybe you could give the choice to the user ?

Owner

jashkenas commented Jun 15, 2012

You're in luck, _.uniq already takes a custom iterator function which can be used to use isEqual semantics, or any other equality semantics you like. Check the docs.

@jashkenas jashkenas closed this Jun 15, 2012

Sorry, it is not a good answer for me.

Try one day in your life a true functionnal language like OCaml, Haskell or Scala and you will see that the default equality test is your isEqual.
We should not to be forced to use an iterator to do that.
The semantic of your function is just false.

So my own fork

@ghost

ghost commented Mar 10, 2013

Try iterator function

x = [['a',1],['b',2],['a',1]]

_.uniq(x,false,function(i){

return i[0] //'a','b'

})
=> [['a',1],['b',2]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment