What’s the closest equivalent to ruby’s Hash#reject/delete_if method?
{a:1, b:2, c:3, d:4}.reject { |k,v| v > 2 }
=> {:a=>1, :b=>2}
this quick version uses key, value of <Object> but it’s not elegant, and lacks the ability to pass an arbitrary condition (as a block or otherwise)
params = {a:1, b:2, c:3}
filter = (hash)->
result = {}
(result[key] = value unless value > 2) for key, value of params
result
console.log(filter(params))
underscore has a similar _.omit method, but only works on keys.
Unfortunately CoffeeScript comprehensions always generate arrays. You could use Underscore’s
objectmethod to transform a[key, value]array into an object:I’d recommend you code your own method for this specific purpose though: