简体   繁体   English

用编程方式填充数组

[英]fill an array with arrays programmatically

Here is a code to create an array of arrays named sims through a for loop and using str1 . 下面是一个代码,用于通过for循环和使用str1创建名为sims的数组数组。

so far I need to define the sims length manually, equal to length of str1 like : let sims = [[],[],[],[]]; 到目前为止,我需要手动定义sims长度,等于str1长度,如: let sims = [[],[],[],[]]; (four arrays equal to four words on str1 ) (四个数组等于str1四个字)

how can I fill sims with arrays programmatically? 如何以编程方式用数组填充sims?

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

You could just split , map and fill an array to accomplish the same output, no need for any for loop at that time 您可以splitmapfill数组以完成相同的输出,此时不需要任何for循环

 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 . 这是使用Array.fromsplit的单行解决方案。

Explanation 说明

  • s1 and s2 are respectively the two strings splitted by a blankspace. s1和s2分别是由空格分割的两个字符串。
  • The first array.from inherits from s1 its length, the second argument is invoked to fill all the values. 第一个array.from从s1继承其长度,调用第二个参数来填充所有值。 For that second argument, we care about the index ( i ). 对于第二个论点,我们关心索引( 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]). 第二个array.from从s2继承它的长度,对于那个我们不关心任何一个参数,因为我们只需要在前一个循环的索引i处的s1元素(所以,s2.length乘以s1 [i ])。 '' + is just the equivalent of toString , which is unneeded, but the main example had it, so... '' +只相当于toString ,这是不需要的,但主要的例子有它,所以......

 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: 您可以通过将空数组推入第一个循环内的sims-array来轻松完成此操作,如下例所示:

 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 pushing之前initializing阵列

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

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

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