javascriptarrayscoffeescriptstatesframerjs

Check if ANY item in an array is in a certain state: coffeescript


I have an array of buttons with a 'selected' and 'deselected' state. How do I go about checking if any button in the array is in the 'selected' state.

So essentially I want something like (using Framer coffeescript):

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

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

Here is my prototype: http://share.framerjs.com/11abcrlne5op/ (go to the ethnicity section).


Solution

  • I'm not sure if by 'any', you mean all of them or some of them, but there are the Array.prototype methods called some, and every.

    Syntax is buttonArray.some(button => button.state.current == "selected")

    This is in javascript, coffescript should have something similar

    You can also use the lodash library's version some and every.

    Or just make a counter in a loop to count them.