Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Simplest way to check if key exists in object using CoffeeScript

In CoffeeScript, what is the simplest way to check if a key exists in an object?

like image 271
ajsie Avatar asked Nov 23 '11 13:11

ajsie


1 Answers

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.

like image 91
Trevor Burnham Avatar answered Oct 10 '22 11:10

Trevor Burnham