test =
[
["qwe","1"],
["ewq","2"],
["aaa","4"],
]我想让数组数量保持在四个,有缺就补空数组,并且要照数组内的数字去补,却哪里补哪里补完要像这样 test =
[
["qwe","1"],
["ewq","2"],
["",""],
["aaa","4"],
]该如何实现这需求?关注 | 3收藏
2 回答
宝慕林4294392
TA贡献2021条经验 获得超8个赞
let test = [ ["qwe", "1"], ["ewq", "2"], ["aaa", "4"], ] function setArr(test) { for (let i = 0; i < test.length; i++) { const item = test[i]; if (i + 1 != item[1]) { test.splice(i, 0, ['', '']) } } return test } console.log(setArr(test))
波斯汪
TA贡献1811条经验 获得超4个赞
test.reduce((acc, cur, i) => { while (cur[1] - 1 != i) { acc.push(['', '']); i++ } acc.push(cur); return acc; }, [])
前提默认原数组是按循序(1,2,3...)排列的
添加回答
举报
0/150
提交
取消