1

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).

aalok89
  • 171
  • 5
  • 14

1 Answers1

1

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.

Omri Luzon
  • 3,975
  • 6
  • 20
  • 29
  • I guess I want 1? ex: I have 10 buttons in my array. I can click each button to cycle states between selected and deselected. If at least 1 button is in the selected state -> run activateMainButton() (this button is not part of the button Array). Here is my prototype. go to the ethnicity section http://share.framerjs.com/11abcrlne5op/ – aalok89 Mar 03 '17 at 08:41
  • Also my programming knowledge is not very advanced, so forgive me. – aalok89 Mar 03 '17 at 08:44
  • So you are looking for the `some` method, which will return true if at least one of the items will return true for the input function you give it. – Omri Luzon Mar 03 '17 at 08:49
  • Quite a bit simpler for the callback to be `button => button.selected`. ;-). Plain JS can be [*embedded in coffeescript*](http://devdocs.io/coffeescript/index#embedded). – RobG Mar 03 '17 at 08:52
  • Gotcha. I think I understand but I'm not quite sure where to put it in my code https://screencast.com/t/EttN36Gg – aalok89 Mar 03 '17 at 08:57
  • Nevermind, got it! Thank you! this search was also helpful https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/every – aalok89 Mar 03 '17 at 09:08