得到数组: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=20list=list.filter(e=>{returnparseInt(e.percent)<=(height+10)&&parseInt(e.percent)>=(height-10)})console.log(list)
ibeautiful
TA贡献1993条经验 获得超5个赞
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);//数据字段偏差范围给出的值
添加回答
举报
0/150
提交
取消