Created
February 1, 2012 23:33
-
-
Save searls/1720156 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
data = | |
a: 0 | |
b: "foo" | |
result = _(data).reject((v,k) -> k == 'b') | |
expect(result).toEqual(a: 0) #but fail! it returns: [0] | |
# so far now, I could settle for this: | |
newData = _(data).clone() | |
_(newData).each (v,k) -> delete newData[k] if k == 'b' |
Right, I'm just using _.reject as an example. Most of the collection functions iterate perfectly well over objects, but only return arrays. That always surprises me.
Yea, It does seem odd. I've hacked together a gist that could become a _(data).object_reject: https://gist.github.com/1720187
A better option would be to extend underscore to make it saner when it's iterating over an object.
Thanks Zach. Yeah, I just hacked my own underscore mixin _.withoutProperties
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
_.reject is a collection method, not an object method.
See: this excerpt from the reject method: (!iterator.call(context, value, index, list)) results[results.length] = value;