js 怎么将数组按照一定条件分组

得到数组:this.dataBarrage=res.list
list:[
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得到这个范围里面的数组?
素胚勾勒不出你
浏览 2819回答 2
2回答

繁星coding

遍历这个对象数组,用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)})console.log(list)

ibeautiful

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;i=(k-offset)&&val
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

JavaScript