Skip to content

Instantly share code, notes, and snippets.

@andrit
Created October 3, 2018 13:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andrit/c8e30f32403c38c2518f4a1f7fe949e1 to your computer and use it in GitHub Desktop.
Save andrit/c8e30f32403c38c2518f4a1f7fe949e1 to your computer and use it in GitHub Desktop.
addCheckboxToSelectedArray = (item, hash) => {
    return new Promise((res, rej) => {
      try{
        let checkboxes = this.state.selectedCheckboxes;
        if(checkboxes.some(e => e.value === item)){
           const findIndexOf = function(arr, val){
             for(let i = 0; i < arr.length; i++) {
               if(arr[i]['value'] === val){
                 return i;
               }
             }
           };
           let indexOf = findIndexOf(checkboxes, item);
           checkboxes.splice(indexOf, 1);
           this.setState({
             selectedCheckboxes: checkboxes
           })
         } else {          
          this.setState({
            selectedCheckboxes: checkboxes.concat({value: item, hashKey: hash})
          })
        }
        res(item);
      }
      catch(e){
        rej(e);
      }
    })
  }
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment