我正在尝试使用1d数组的内容创建2d数组。 2d数组具有正确的行数和列数,但是2d数组的每个索引都包含原始1d数组的全部。
如何旋转一维数组:
var OneD = [ "Bristol", "Cardiff", "Birmingham",
"Luton", "Swansea", "Aberdeen",
"Birmingham", "Manchester", "Southampton",
"Chester", "Swansea", "Brighton",
"Portsmouth", "Bournemouth", "Glasgow",
"Newcastle", "Cardiff", "Bristol"];
进入2d数组:
twoD = [
["Bristol", "Cardiff", "Birmingham", "Luton", "Swansea", "Aberdeen"],
["Birmingham", "Manchester", "Southampton", "Chester", "Swansea", "Brighton"],
["Portsmouth", "Bournemouth", "Glasgow", "Newcastle", "Cardiff", "Bristol"]
];
我的代码
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);
答案 0 :(得分:1)
使用单个for循环。在每次迭代中,将cols
编号添加到i
,并从OneD
数组中切出i
和i
+ cols
之间的项。将切片的项目推入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);
答案 1 :(得分:0)
这样可以做到。但请务必处理遗漏的物品。这里的数组长度正好是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);
(或)
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);
答案 2 :(得分:0)
您的第二个循环每次都推入所有OneD
。您需要索引数组。
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);
您还可以将两个循环组合为一个循环。并且通过使外循环按列数递增来摆脱索引计算。下面的版本也不要求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);