为了账号安全,请及时绑定邮箱和手机立即绑定

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条经验 获得超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);//数据字段偏差范围给出的值
                            
查看完整回答
反对 回复 2019-05-11
  • 2 回答
  • 0 关注
  • 2818 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信