检查数组中是否有咖啡脚本中的某些项目 [英] Checking if an array has some items in coffeescript

查看:12
本文介绍了检查数组中是否有咖啡脚本中的某些项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当数组中有一些项目时,coffeescript 中是否有一些方法返回 true?像 ruby​​ 中的方法 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.htmlcoffeescript 中的数组空性由它的长度决定

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

这对我来说太蹩脚了.

推荐答案

我认为没有但可以:

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

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

一个非常简单和不完整的实现,但它应该给你一些想法.并且记录一下,Ruby 中没有 present? 方法,该方法是由 active_support gem 添加的.

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.

这篇关于检查数组中是否有咖啡脚本中的某些项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆