亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

js 怎么將數組按照一定條件分組

js 怎么將數組按照一定條件分組

素胚勾勒不出你 2019-05-11 09:34:08
得到數組:this.dataBarrage=res.listlist:[0:{id:"1",content:"ddddd",percent:"60"}1:{id:"1",content:"ddddd",percent:"30"}2:{id:"2",content:"葫蘆娃",percent:"0"}3:{id:"3",content:"本領大",percent:"10"}4:{id:"4",content:"Qwery",percent:"0"}比如我在界面上獲取的一個高度值為20,那么我怎么獲取這個數組里面percent的值為10-30范圍的值,就是我要根據我的高度將這個數組分組。這個高度值已經獲取為this.scrollStop。怎么根據這個高度值計算this.scrollStop-10到this.scrollStop+10得到這個范圍里面的數組?
查看完整描述

2 回答

?
繁星coding

TA貢獻1797條經驗 獲得超4個贊

遍歷這個對象數組,用array.filter就可以了
letlist=[
{id:"1",content:"ddddd",percent:"60"},
{id:"1",content:"ddddd",percent:"30"},
{id:"2",content:"葫蘆娃",percent:"0"},
{id:"3",content:"本領大",percent:"10"},
{id:"4",content:"Qwery",percent:"0"},
]
letheight=20
list=list.filter(e=>{
returnparseInt(e.percent)<=(height+10)&&parseInt(e.percent)>=(height-10)
})
console.log(list)
                            
查看完整回答
反對 回復 2019-05-11
?
ibeautiful

TA貢獻1993條經驗 獲得超6個贊

varlist=[{id:"1",content:"ddddd",percent:"60"},
{id:"1",content:"ddddd",percent:"30"},
{id:"2",content:"葫蘆娃",percent:"0"},
{id:"3",content:"本領大",percent:"10"},
{id:"4",content:"Qwery",percent:"0"},
{id:"5",content:"ddddd",percent:"80"},
{id:"6",content:"ddddd",percent:"90"},
{id:"7",content:"ddddd",percent:"50"},
]
functionsearch(data,key,offset,val){
varnewArr=[];
for(vari=0;ivark=parseInt(data[i][key]);
if(val>=(k-offset)&&val<=(k+offset))
newArr.push(data[i]);
}
returnnewArr;
}
search(list,'percent',10,20);//數據字段偏差范圍給出的值
                            
查看完整回答
反對 回復 2019-05-11
  • 2 回答
  • 0 關注
  • 2862 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號