I'm looking for a method for javascript returns true or false when it's empty.. something like Ruby any?
or empty?
[].any? #=> false
[].empty? #=> true
I'm looking for a method for javascript returns true or false when it's empty.. something like Ruby
|
||||
|
Javascript standard library is quite poor compared to ruby's Enumerable, most stuff should be written by hand. On the bright side, this is trivial in most cases.
Libraries like underscore already provide wrappers like these, if you're used to ruby's collection methods, it makes sense to include them in your project. As @Pointy pointed out, JS arrays are not lists, they are just objects with numeric keys and |
|||||||||||||||||||||
|
If you really want to got nuts, add a new method to the prototype:
|
|||||||||||||||||
|
JavaScript arrays can be "empty", in a sense, even if the length of the array is non-zero. For example:
The variable "howMany" will be set to Thus because many of the Array iteration functions only pay attention to elements of the array that have actually been assigned values, you can use something like this call to
The callback passed to |
|||
|
Actually you are looking for javascript native
|
|||||
|
Array has a length property :
|
|||
|
I would just check the length. You could potentially wrap it in a helper method if you like. |
|||
|
Just use
See MDN |
|||
|