【问题标题】:compare two arrays in reactjs [closed]比较reactjs中的两个数组[关闭]
【发布时间】:2017-06-13 18:57:50
【问题描述】:

我有这个代码:在 reactjs http://codepen.io/fernandooj/pen/MJOodr

1- list an array, when mapping I send each item a checkbox
2- I have another array that I still do not have
3- I need to compare the two arrays and if there is an element of both arrays 

等于列表中重复的复选框处于活动状态 例子 数组1 [1,2,3] 阵列 2 [2] 复选框“2”出现勾选

【问题讨论】:

    标签: arrays reactjs checkbox


    【解决方案1】:

    您可以将call a function with item 作为参数,因此defaultChecked 的值。确保不要绑定函数,因为 defaultChecked 需要一个值而不是函数。

      getChecked(item) {
         console.log(item);
         if(array2.indexOf(item) > -1) {return true}
         return false;
       }
           <input 
                key={item} 
                name='list' 
                type='checkbox' 
                onClick={(e)=>this.updateStateList(e,item)}
                label='Add' 
                className='listTour'
                defaultChecked={this.getChecked(item)}/>
    

    CodePen

    【讨论】:

      猜你喜欢
      • 2013-08-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-05-03
      • 2010-11-09
      相关资源
      最近更新 更多