将一维数组转换为二维数组
问题描述:
我正在开发一个程序,必须将文本文件中的值读取到一维数组中.我已经成功获取了该一维数组中的数字.
I am working on a program where I have to read the values from a textfile into an 1D array.I have been succesfull in getting the numbers in that 1D array.
m1=[1,2,3,4,5,6,7,8,9]
但我希望数组为
m1=[[1,2,3],[4,5,6],[7,8,9]]
答
您可以使用以下代码:
const arr = [1,2,3,4,5,6,7,8,9];
const newArr = [];
while(arr.length) newArr.push(arr.splice(0,3));
console.log(newArr);