Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 检查数组中是否有coffeescript中的某些项_Arrays_Coffeescript - Fatal编程技术网

Arrays 检查数组中是否有coffeescript中的某些项

Arrays 检查数组中是否有coffeescript中的某些项,arrays,coffeescript,Arrays,Coffeescript,在coffeescript中是否有某种方法在数组中包含某些项时返回true?与ruby中的方法类似present?: [].present? false [1].present? true 根据数组,coffeescript中的空值由其长度决定 alert("Empty Array") unless [].length 这对我来说太差劲了。不幸的是,没有。最好的方法是比较它的长度。我认为没有,但可以: Array::present = -> @.length > 0 if

在coffeescript中是否有某种方法在数组中包含某些项时返回true?与ruby中的方法类似
present?

[].present? false
[1].present? true
根据数组,coffeescript中的空值由其长度决定

alert("Empty Array")  unless [].length

这对我来说太差劲了。

不幸的是,没有。最好的方法是比较它的长度。

我认为没有,但可以:

Array::present = ->
  @.length > 0

if [42].present()
  # why yes of course
else
  # oh noes

这是一个非常简单且不完整的实现,但它应该会给您一些想法。作为记录,Ruby中没有
present?
方法,该方法是由
active\u支持
gem添加的。

我认为在
中使用
也可以

arr = [1, 2, 3, 4, 5]
a = 1
if a in arr
  console.log 'present'
else
  console.log 'not present'

Output
$ present

为了清楚起见,我会选择
empty()
(比如Ruby中的
empty?
)。不那么可疑的IMHO。@Cimm我认为这两种方法都是Ruby的Array类中的方法,而且从我所看到的情况来看,这两种方法都很常见。我认为您不理解这个问题。