test=[["qwe","1"],["ewq","2"],["aaa","4"],]我想让数组数量保持在四个,有缺就补空数组,并且要照数组内的数字去补,却哪里补哪里补完要像这样test=[["qwe","1"],["ewq","2"],["",""],["aaa","4"],]该如何实现这需求?
2 回答
江户川乱折腾
TA贡献1851条经验 获得超5个赞
lettest=[["qwe","1"],["ewq","2"],["aaa","4"],]functionsetArr(test){for(leti=0;iconstitem=test[i]; if(i+1!=item[1]){test.splice(i,0,['',''])}}returntest}console.log(setArr(test))
翻翻过去那场雪
TA贡献2065条经验 获得超14个赞
test.reduce((acc,cur,i)=>{while(cur[1]-1!=i){acc.push(['','']);i++}acc.push(cur);returnacc;},[])前提默认原数组是按循序(1,2,3...)排列的
添加回答
举报
0/150
提交
取消