【问题标题】:How Add or Remove Checkboxes value from the array?如何从数组中添加或删除复选框值?
【发布时间】:2018-06-28 08:47:48
【问题描述】:

我想从数组中添加或删除值。 当我选择复选框时,值将推送到数组。 当我取消选择复选框时,该值将从数组中删除

复选框

<CheckBox
 checked={this.state.currentValue}
onClick={() => this._changeValue(index)}>

点击功能

_changeValue(value) {
    this.setState({
      currentValue: !this.state.currentValue,
    });
   console.log(value)
   selectedQuesiton.push(value)
  }

【问题讨论】:

  • 你能把你创建的代码提供给我吗?
  • 已经提供的代码..

标签: javascript react-native checkbox checkboxlist native-base


【解决方案1】:

我就是这样的

_changeValue(value) {
   this.setState({
     currentValue: !this.state.currentValue,
   });
   if(selectedQuesiton.includes(value)){
       selectedQuesiton = selectedQuesiton.filter(item => item !== value)
       }
       else{
         selectedQuesiton.push(value)
       }
       console.log(selectedQuesiton)
 }

我正在使用过滤器来检查数组中的数据是否可用,如果是则 它只会忽略该项目到新数组中,在您的情况下选择Quesiton

希望这行得通,如果没有,请评论我

【讨论】:

    猜你喜欢
    • 2022-08-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    • 1970-01-01
    相关资源
    最近更新 更多