I'm trying to create a 2d array with the contents of a 1d array. The 2d array has the correct amount of rows and columns, however, each index of the 2d array contains the entirety of the original 1d array.
How can I turn the 1d array:
var OneD = [ "Bristol", "Cardiff", "Birmingham",
"Luton", "Swansea", "Aberdeen",
"Birmingham", "Manchester", "Southampton",
"Chester", "Swansea", "Brighton",
"Portsmouth", "Bournemouth", "Glasgow",
"Newcastle", "Cardiff", "Bristol"];
Into this 2d array:
twoD = [
["Bristol", "Cardiff", "Birmingham", "Luton", "Swansea", "Aberdeen"],
["Birmingham", "Manchester", "Southampton", "Chester", "Swansea", "Brighton"],
["Portsmouth", "Bournemouth", "Glasgow", "Newcastle", "Cardiff", "Bristol"]
];
My Code
var twoD = [];
var rows = 3;
var cols = 6;
for (var i = 0; i < rows; i++) {
twoD.push( [] );
}
for (var i = 0; i < rows; i++) {
for (var j = 0; j < cols; j++) {
twoD[i].push(oneD);
}
}
console.log(twoD);