数组里面的状态为true和false,我想把为前三个为true的,就为true,后面的还有true

现在我的数组里面有name 和 status


 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},

   ]

        console.log(data);

        

我想把从第一个开始算,一直到出现过三个true,然后把剩下的都变为false,这个怎么实现呢?


有只小跳蛙
浏览 862回答 4
4回答

侃侃尔雅

let count = 0;for(let item of data){&nbsp; &nbsp; if(count < 3){&nbsp; &nbsp; &nbsp; &nbsp; item.status && ++count&nbsp; &nbsp; }else{&nbsp; &nbsp; &nbsp; &nbsp; item.status = false;&nbsp; &nbsp; }}

缥缈止盈

&nbsp; &nbsp; &nbsp; &nbsp; let data = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小明", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小红", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小张", status: true },//一次出现&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小墙", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小粉", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小军", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小钰", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小访", status: true },//二次出现&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小源", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小方式", status: false },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小佛", status: true },//三次出现&nbsp; 三次之后&nbsp; 还有true的变为false&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小付款", status: true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小哦咯", status: true },&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; { name: "小来了", status: false },&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; &nbsp; &nbsp; let count = 0;&nbsp; &nbsp; &nbsp; &nbsp; data.forEach(obj => {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (count >= 3) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; obj.status = false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (obj.status) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; count++;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; })&nbsp; &nbsp; &nbsp; &nbsp; console.log(data);

动漫人物

let total = 0;let result = data.map(k => {&nbsp; &nbsp; &nbsp;&nbsp; &nbsp;if(k.status) {total++};&nbsp; &nbsp;if(total>3 && k.status) {k.status=false}&nbsp; &nbsp;return k;});

LEATH

data.reduce((count, cur) => {&nbsp; return count < 3 && cur.status ? ++count : (cur.status = false, count)}, 0)console.log(data)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript