【问题标题】:Check if ANY item in an array is in a certain state: coffeescript检查数组中的任何项目是否处于某种状态:coffeescript
【发布时间】:2017-07-23 06:35:26
【问题描述】:

我有一组具有“选中”和“取消选中”状态的按钮。如何检查数组中的任何按钮是否处于“选定”状态。

所以基本上我想要类似的东西(使用 Framer 咖啡脚本):

for button in buttonArray
    button.onClick ->
       this.stateCycle("selected", "default")

       if any button in buttonArray state.current == "selected"
           activateMainButton()
       else
           deactivateMainButton()

这是我的原型:http://share.framerjs.com/11abcrlne5op/(转到种族部分)。

【问题讨论】:

    标签: javascript arrays coffeescript states framerjs


    【解决方案1】:

    我不确定“任何”是指全部还是其中一些,但有称为 someeveryArray.prototype 方法。

    语法是buttonArray.some(button => button.state.current == "selected")

    这是在javascript中,咖啡脚本应该有类似的东西

    您也可以使用 lodash 库的版本 someevery

    或者只是在循环中创建一个计数器来计算它们。

    【讨论】:

    • 我想我想要 1?例如:我的阵列中有 10 个按钮。我可以单击每个按钮来循环选择和取消选择之间的状态。如果至少有 1 个按钮处于选中状态 -> 运行 activateMainButton() (此按钮不是按钮数组的一部分)。这是我的原型。转到种族部分share.framerjs.com/11abcrlne5op
    • 另外我的编程知识不是很高级,请见谅。
    • 所以您正在寻找some 方法,如果至少有一项为您提供的输入函数返回true,则该方法将返回true。
    • button => button.selected 的回调要简单得多。 ;-)。纯JS可以是embedded in coffeescript
    • 明白了。我想我明白了,但我不太确定在我的代码中放在哪里screencast.com/t/EttN36Gg
    猜你喜欢
    • 1970-01-01
    • 2013-12-01
    • 1970-01-01
    • 1970-01-01
    • 2020-03-02
    • 1970-01-01
    • 2020-01-11
    • 2019-12-10
    • 1970-01-01
    相关资源
    最近更新 更多