0

我有下面两个数组,
我想看下如果b数组下的namea数组下有的话我就让他的flag=true

var a = ['a','b','c'];
var b = [
    {name: 'a',flag: false,},
    {name: 'b',flag: false,},
    {name: 'c',flag: false,},
    {name: 'd',flag: false,}
]

6个回答

1

for(let _b of b){_b.flag = a.indexOf(_b.name) > -1;}

1
b = b.map(function (obj) {
    if (a.indexOf(obj.name) >= 0) {
        obj.flag = true;
    }
    return obj;
});
1
b.forEach(v=>a.indexOf(v.name)!==-1 && (v.flag=true))
0

两层循环

for(var i = 0 ; i< b.length ; i++){
    for(var j = 0;j < a.length ; j ++){
        if(b[i].name == a[j]){
            b[i].flag = true;
        }
    }
}
0

b = b.map(item => {return item.flag = a.indexOf(item.name) > -1 })

-1
for(var i=0,len=a.length;i<len;i++){
    if(b[i].name==a[i]){
        flag=true;
        //so?
    }
}

楼主你想干嘛?

撰写答案