简体   繁体   English

将数据从1d阵列推送到2d阵列

[英]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. 我正在尝试使用1d数组的内容创建一个二维数组。 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. 2d数组具有正确的行数和列数,但是,2d数组的每个索引都包含原始1d数组的全部。

How can I turn the 1d array: 如何打开1d数组:

var OneD = [ "Bristol", "Cardiff", "Birmingham",
             "Luton", "Swansea", "Aberdeen",
             "Birmingham", "Manchester", "Southampton", 
             "Chester", "Swansea", "Brighton",
             "Portsmouth", "Bournemouth", "Glasgow", 
             "Newcastle", "Cardiff", "Bristol"];

Into this 2d array: 进入这个2d数组:

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. 使用单个for循环。 On each iteration, add the cols number to i , and slice from the OneD array the items between i and i + cols . 在每次迭代中,将cols编号添加到i ,并从OneD阵列切片ii + cols之间的项目。 Push the sliced items into the twoD array. 将切片的项目推入twoD数组。

 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. 这里的数组长度恰好是6个项目的3个部分。

 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. 你的第二个循环是每次都推动所有的OneD 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. 下面的版本也不要求1d数组的长度恰好是rows * cols

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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