问题描述
有没有一些CoffeeScript的方法时,阵列中有一些项目,则返回true?像红宝石 present
方法:
Is there some method in coffeescript that returns true when an array has some items in it? Like method in ruby present?
:
[].present? false
[1].present? true
据http://arcturo.github.com/library/coffeescript/07_the_bad_parts.html在CoffeeScript中数组空虚是由它的长度来确定
According to http://arcturo.github.com/library/coffeescript/07_the_bad_parts.html an array emptiness in coffeescript is determined by its length
alert("Empty Array") unless [].length
这似乎很蹩脚我。
推荐答案
我不觉得有什么,但可以是:
I don't think there is but can be:
Array::present = ->
@.length > 0
if [42].present()
# why yes of course
else
# oh noes
有一个非常简单的和不完整的实现,但它应该给你一些想法。并记录在案,没有 present?
方法在Ruby中,该方法是由 active_support
宝石加
A very simple and incomplete implementation but it should give you some ideas. And for the record, there's no present?
method in Ruby, the method is added by the active_support
gem.
这篇关于检查如果一个数组中的CoffeeScript一些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!