vue框架下
this.AllAuthorityOptions=["11","22","33","44","55","管理"];
this.checkedAuthority=["11","33","管理"]
用数组this.checked[index]值记录判断结果,相等true,不等false;,如下设置默认false:
for(let item of this.AllAuthorityOptions) {
this.checked.push(false);//设置默认false
}
判断两个数组的相同项:
方法1用indexof:
for(var i=0;i<this.Options.length;i++){
if(this.checkedAuthority.indexOf(this.Options[i]) != -1){
this.checked[i]=true;
}else{
this.checked[i]=false;
}
}
console.log(this.checked+'this.checked111');
方法2用vue.map():
this.Options.map((item,index)=>{
this.checkedAuthority.map((item1,index1)=>{//再判断有相同项的状态为1
if(item == item1){
this.checked[index] = true;
}
})
})
结果:console.log(this.checked);//[true,true,false,false,false,true]
如果数组中的元素是对象,方式如下:
数组1
var arr1 = [
{id:'1',name:'第一项'},
{id:'2',name:'‘第二项’'},
{id:'3',name:'第三项 ' },
];
数组2
var arr2 = [
{id:'1',name:'第一项'},
{id:'3',name:'第二项},
]
var arr = arr1.map((item,index)=>{
item.state = 0;//先设置所有的状态为0
arr2.map((item1,index1)=>{//再判断有相同项的状态为1
if(item.name == item1.name){
item.state = 1;
}
})
return item;
})
结果:console.log(arr)可见:[{id:'1',name:'第一项','state':'1'},{id:'1',name:'第一项','state':'0'},{id:'1',name:'第一项','state':'1'}]
所有评论(0)