In CoffeeScript, what is the simplest way to check if a key exists in an object?
key of obj
This compiles to JavaScript's key in obj
. (CoffeeScript uses of
when referring to keys, and in
when referring to array values: val in arr
will test whether val
is in arr
.)
thejh's answer is correct if you want to ignore the object's prototype. Jimmy's answer is correct if you want to ignore keys with a null
or undefined
value.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With