现在有一个数组如下var arr = [111,23,44,534,4123,312,32,3123,424,552,24,55];每隔三个切割一次 形成一个新的数组,[[111,23,44],[534,4123,312],[32,3123,424],[24,55]]再在新的数组里取最大值和index 形成最终数组arr = arr=[{'index':0,'max':111},{'index':1,'max':4123},{'index':2,'max':3123},{'index':3,'max':55}];请问 如何做到
1 回答
ibeautiful
TA贡献1993条经验 获得超5个赞
var arr = [111,23,44,534,4123,312,32,3123,424,552,24,55];
var result = [];
for(var i=0,j=0; i<arr.length; i+=3,j++){
result.push({'index': j, 'max': Math.max.apply(Math, arr.slice(i,i+3))});
}
console.log(result);
添加回答
举报
0/150
提交
取消