4 回答
TA贡献1801条经验 获得超16个赞
let count = 0;
for(let item of data){
if(count < 3){
item.status && ++count
}else{
item.status = false;
}
}
TA贡献2041条经验 获得超4个赞
let data = [
{ name: "小明", status: false },
{ name: "小红", status: false },
{ name: "小张", status: true },//一次出现
{ name: "小墙", status: false },
{ name: "小粉", status: false },
{ name: "小军", status: false },
{ name: "小钰", status: false },
{ name: "小访", status: true },//二次出现
{ name: "小源", status: false },
{ name: "小方式", status: false },
{ name: "小佛", status: true },//三次出现 三次之后 还有true的变为false
{ name: "小付款", status: true },
{ name: "小哦咯", status: true },
{ name: "小来了", status: false },
];
let count = 0;
data.forEach(obj => {
if (count >= 3) {
obj.status = false;
return;
}
if (obj.status) {
count++;
}
})
console.log(data);
TA贡献1815条经验 获得超10个赞
let total = 0;
let result = data.map(k => {
if(k.status) {total++};
if(total>3 && k.status) {k.status=false}
return k;
});
TA贡献1936条经验 获得超6个赞
data.reduce((count, cur) => {
return count < 3 && cur.status ? ++count : (cur.status = false, count)
}, 0)
console.log(data)
添加回答
举报