最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - fill an array with arrays programmatically - Stack Overflow

programmeradmin1浏览0评论

Here is a code to create an array of arrays named sims through a for loop and using str1.

so far I need to define the sims length manually, equal to length of str1 like : let sims = [[],[],[],[]]; (four arrays equal to four words on str1)

how can I fill sims with arrays programmatically?

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

Here is a code to create an array of arrays named sims through a for loop and using str1.

so far I need to define the sims length manually, equal to length of str1 like : let sims = [[],[],[],[]]; (four arrays equal to four words on str1)

how can I fill sims with arrays programmatically?

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

Share edited Aug 6, 2019 at 11:49 adiga 35.3k9 gold badges65 silver badges87 bronze badges asked Aug 6, 2019 at 11:42 Sara ReeSara Ree 3,5431 gold badge19 silver badges72 bronze badges
Add a ment  | 

5 Answers 5

Reset to default 5

You could just split, map and fill an array to acplish the same output, no need for any for loop at that time

var str1 = "do you ever looked";
var str2 = "do you fr ever looked";

let subArrayLength = str2.split(" ").length;
let sims = str1.split(' ').map( value => new Array( subArrayLength ).fill( value ) );
console.log(sims);

Here is a single line solution using just Array.from and split.

Explanation

  • s1 and s2 are respectively the two strings splitted by a blankspace.
  • The first array.from inherits from s1 its length, the second argument is invoked to fill all the values. For that second argument, we care about the index (i).
  • The second array.from inherits from s2 its length, for that one we don't care about either of the arguments, since we will just need the s1 element at index i of the previous loop (so, s2.length times s1[i]). '' + is just the equivalent of toString, which is unneeded, but the main example had it, so...

var str1 = "do you ever looked", s1 = str1.split(' ');
var str2 = "do you fr ever looked", s2 = str2.split(' ');

let sims = Array.from(s1, (_,i) => Array.from(s2, () => '' + s1[i]));
console.log(sims);

You could easily get this done by pushing empty arrays into your sims-array inside your first loop, like in the example below:

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.push([]); // this does the trick :-)
  for (var i = 0; i < s2.length; i++) {
    sims[j].push(s1[j].toString());
  }

}

console.log(sims);

Do initializing the array before 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);

One way :

var s1 = str1.split(" ")
let array = []
for(var i = 0; i < s1.length; s1++) array.push(new Array())
发布评论

评论列表(0)

  1. 暂无评论