coffeescript - 检查数组中是否存在具有特定属性值的对象

标签 coffeescript

我仍在努力学习 CoffeeScript,所以我只是想问您如何以更简单的方式编写这段代码。

for member in draft.members
        if member.email is emailAddress then throw new Meteor.Error 500, "Cannot add member twice"

最佳答案

您还可以使用 Array#some ,这会产生非常简单的 JS,并且不需要外部库 :)

emailAlreadyUsed = draft.members.some (m) -> m.email is emailAddress
throw new Meteor.Error 500, 'Cannot add member twice' if emailAlreadyUsed

关于coffeescript - 检查数组中是否存在具有特定属性值的对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16119549/

相关文章:

Javascript/Jquery .html() 仅替换一次

javascript - 将 @states 和 props 传递给弹出模型

javascript - 三级继承不起作用

python - 将变量插入Python和CoffeeScript中的字符串

javascript - 在 Backbone 中,如何在所有 View 上使用 after_render() ?

javascript - MeteorJS 和 Coffeescript : "unexpected . "

javascript - Coffeescript watch src 目录进行编译

javascript - JS/CoffeeScript : Element doesn't receive focus on . 焦点()

syntax - 相当于python 'pass'语句的Coffeescript

windows - 为什么我在 cygwin 中运行咖啡命令时出现错误?