比如arr1 = [2,2,2,5,5,6,6,6,6,7];按相同拆分想得到arr2 = [[2,2,2],[5,5],[6,6,6,6],[7]];
3 回答
一只名叫tom的猫
TA贡献1906条经验 获得超3个赞
let arr1 = [2,2,2,5,5,6,6,6,6,7];
function trans (arr) {
let pre = NaN
let result = []
let curArr
arr.forEach(cur => {
if (cur === pre) {
curArr.push(cur)
} else {
curArr = [cur]
result.push(curArr)
pre = cur
}
})
return result
}
trans(arr1)
Qyouu
TA贡献1786条经验 获得超11个赞
[2,2,2,5,5,6,6,6,6,7]
.reduce((m,n) => (m[n] = (m[n] || []).concat(n), m),[])
.filter(a => a)
- 3 回答
- 0 关注
- 4350 浏览
添加回答
举报
0/150
提交
取消