简体   繁体   中英

Pushing data from a 1d array to a 2d array

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

Use a single for loop. On each iteration, add the cols number to i , and slice from the OneD array the items between i and i + cols . Push the sliced items into the twoD array.

 var OneD = ["Bristol","Cardiff","Birmingham","Luton","Swansea","Aberdeen","Birmingham","Manchester","Southampton","Chester","Swansea","Brighton","Portsmouth","Bournemouth","Glasgow","Newcastle","Cardiff","Bristol"]; var twoD = []; var cols = 6; for(var i = 0; i < OneD.length; i += cols) { twoD.push(OneD.slice(i, i + cols)); } console.log(twoD); 

This way it can be done. But be sure to handle left out items. here the array length is exactly 3 parts for 6 items.

 var OneD = ["Bristol", "Cardiff", "Birmingham", "Luton", "Swansea", "Aberdeen", "Birmingham", "Manchester", "Southampton", "Chester", "Swansea", "Brighton", "Portsmouth", "Bournemouth", "Glasgow", "Newcastle", "Cardiff", "Bristol" ]; var TwoD = []; var cols = 6; var rows = parseInt(OneD.length / cols, 10); // rows = 3 for (var i = 0; i < rows; i++) { TwoD.push(OneD.slice(i * cols, (i * cols + cols))); } console.log(TwoD); 

(Or)

 var OneD = ["Bristol", "Cardiff", "Birmingham", "Luton", "Swansea", "Aberdeen", "Birmingham", "Manchester", "Southampton", "Chester", "Swansea", "Brighton", "Portsmouth", "Bournemouth", "Glasgow", "Newcastle", "Cardiff", "Bristol" ]; var TwoD = OneD.join(',').match(/([^,]*,[\\w]*){5}[,]*/g).map(function(i) {return i.replace(/,$/, '').split(/,/);}); console.log(TwoD); 

Your second loop is pushing all of OneD every time. You need to index the array.

 var oneD = ["Bristol","Cardiff","Birmingham","Luton","Swansea","Aberdeen","Birmingham","Manchester","Southampton","Chester","Swansea","Brighton","Portsmouth","Bournemouth","Glasgow","Newcastle","Cardiff","Bristol"]; var twoD = []; var cols = 6; var rows = 3; 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[i * cols + j]); } } console.log(twoD); 

You can also combine the two loops into one. And also get rid of the index calculation by making the outer loop increment by the number of columns. The version below also doesn't require the 1d array to be exactly rows * cols in length.

 var oneD = ["Bristol","Cardiff","Birmingham","Luton","Swansea","Aberdeen","Birmingham","Manchester","Southampton","Chester","Swansea","Brighton","Portsmouth","Bournemouth","Glasgow","Newcastle","Cardiff","Bristol"]; var twoD = []; var cols = 6; var rows = 3; for (var i = 0; i < oneD.length; i += cols) { var row = []; var maxColIndex = Math.min(i+cols, oneD.length); for (var j = i; j < maxColIndex; j++) { row.push(oneD[j]); } twoD.push(row); } console.log(twoD); 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM