在JavaScript中,我有这种情况:地图为阵列,并比较至少一种巧合
this.props.number
始终是一个数字0,1或2
this.props.columnas
是这样的对象的数组:
[
{value:'1',text:'AA'},
{value:'2',text:'BB'},
{value:'3',text:'CC'},
{value:'4',text:'DD'},
{value:'5',text:'EE'},
{value:'6',text:'FF'},
{value:'7',text:'GG'}
]
selected
是对象数组太像这样:
[{col:"2", ope:"", val:""}, {col:"5", ope:"", val:""}, {col:"7", ope:"", val:""}, ]
而且我有这样的代码:
this.props.columnas.map(function(col){
if(this.props.number == 0) {
if (col.value == selected[0].col)
disabledIndice = true
else
disabledIndice = false;
}
else if(this.props.number == 1) {
if (col.value == selected[0].col || col.value == selected[1].col)
disabledIndice = true
else
disabledIndice = false;
}
else if(this.props.number == 2) {
if (col.value == selected[0].col || col.value == selected[1].col || col.value == selected[2].col)
disabledIndice = true
else
disabledIndice = false;
}
return <option value={col.value} disabled={disabledIndice}>{col.text}</option>
}.bind(this))}
(忘了bind
。 function
和es5,我会用箭头功能)
它太长了,它没有做我需要做的。我想再次使用地图selected
来查找哪个col.value
与存储在selected
上的地址相同。
在上面的代码中,我假设我将有3个对象selected
,但情况并非总是如此。有时可能是1,2或3个对象,即每个案例的s why the code is using those ìf
(对象的数量由this.props.number
定义)。这很丑陋。
我已经试过这样的事情:
this.props.columnas.map(function(col){
indicesYaSeleccionado.map(function(item){
if (col.value == item.col)
disabledIndice = true
else disabledIndice = false;
});
return <option key={constant.guid()} value={col.value} disabled={disabledIndice}>{col.text}</option>
}.bind(this))
但是,我总是会得到disabled
因为我比较每个值,我需要至少1个巧合设置为disabledIndice = true
。
任何提示?这是一个长时间的解释,对不起。
你能清楚地提到你有什么期待地图的最终结果是什么? –
该代码设置了