这是通过for循环并使用sims
创建名为str1
的数组数组的代码。
到目前为止,我需要手动定义sims
的长度,等于str1
的长度,例如:let sims = [[],[],[],[]];
(四个数组,等于str1
上的四个单词)
如何以编程方式用数组填充模拟人生?
var str1 = "do you ever looked";
var str2 = "do you fr ever looked";
let sims = [[],[],[],[]]; // instead I want let sims = [];
let s1 = str1.split(" ")
let s2 = str2.split(" ")
for (var j = 0; j < s1.length; j++) {
for (var i = 0; i < s2.length; i++) {
sims[j].push(s1[j].toString());
}
}
console.log(sims);
最佳答案
在initializing
之前先对数组进行pushing
sims[j] = [];
var str1 = "do you ever looked";
var str2 = "do you fr ever looked";
let sims = [];
let s1 = str1.split(" ")
let s2 = str2.split(" ")
for (var j = 0; j < s1.length; j++) {
sims[j] = [];
for (var i = 0; i < s2.length; i++) {
sims[j].push(s1[j].toString());
}
}
console.log(sims);
关于javascript - 以编程方式用数组填充数组,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57375273/