Monday, May 20, 2024
 Popular · Latest · Hot · Upcoming
92
rated 0 times [  98] [ 6]  / answers: 1 / hits: 51976  / 10 Years ago, mon, march 17, 2014, 12:00:00

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]


but I want the array to be



m1=[[1,2,3],[4,5,6],[7,8,9]]

More From » arrays

 Answers
56

You can use this code :




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);




http://jsfiddle.net/JbL3p/


[#71939] Sunday, March 16, 2014, 10 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
austonjuancarlosb

Total Points: 238
Total Questions: 89
Total Answers: 99

Location: Chad
Member since Mon, Dec 5, 2022
1 Year ago
;