简体   繁体   English

创建带有子子数组的函数

[英]Function that creates an array with subarries

I have a function which creates an array of subarrays. 我有一个创建子数组数组的函数。 It takes three parameters: the number of subarries to create within the array ( numSub ), the number of times the desired value occurs within each subarray ( numValue ), and the desired value ( value ). 它有三个参数:subarries的数量到阵列(内创建numSub ),每个子阵列(内出现所期望的值的次数numValue ),以及所需的值( value )。

For example, if I were to call the function with (3, 2, test) , I would want the following output: 例如,如果要使用(3, 2, test)调用该函数,则需要以下输出:

Array [Array ["test", "test"], Array ["test", "test"], Array ["test", "test"]]

Here is the function I have written: 这是我编写的函数:

 function subarrs(numSub, numValue, value) { let arr = []; for (let i = 0; i < numSub; i++) { arr.push([]); } arr.forEach(function(sub) { sub.fill(value, 0, numValue - 1); }); return arr; } console.log(subarrs(3, 2, 'test')); 

I have looped through the numSub variable and inserted an array into arr for each iteration. 我遍历了numSub变量,并为每次迭代将数组插入到arr中。 This was successful. 这是成功的。 ( Array [Array [], Array [], Array []] ) Array [Array [], Array [], Array []]

I then use a forEach loop to fill each sub-array with value beginning at index 0 and ending at index numValue - 1 (because the second occurrence of the value would actually be at index 1.) 然后,我使用forEach循环以从索引0开始到索引numValue - 1结束的value填充每个子数组(因为第二次出现的值实际上是在索引1处。)

The function does not work as intended, however. 但是,该功能无法正常工作。 Rather than the aforementioned desired output, I receive this: 我收到的不是上述所需的输出,而是:

Array [Array [], Array [], Array []]

You can use fill on an array that has received the right length, like Array(numValue).fill(numValue) . 您可以在长度合适的数组上使用fill ,例如Array(numValue).fill(numValue) Here is how you could do it: 这是您可以执行的操作:

 function subarrs(numSub, numValue, value) { return Array.from({length: numSub}, () => Array(numValue).fill(value)); } console.log(subarrs(3, 2, 'test')); 

You are filling an empty array. 您正在填充一个空数组。 It's still an empty array (nothing to fill). 它仍然是一个空数组(什么也没填)。

You should construct the array of some length: 您应该构造一些长度的数组:

arr.push(new Array(numValue));

Complete: 完成:

function subarrs(numSub, numValue, value) {
  let arr = [];
  for (let i = 0; i < numSub; i++) {
    arr.push(new Array(numValue));
  }
  arr.forEach(function(sub) {
    sub.fill(value);
  });
  return arr;
}

console.log(subarrs(3, 2, 'test'));

Array.fill() only modifies array values. Array.fill()仅修改数组值。 It does not add new ones. 它不会添加新的。 Use push again instead 再次使用推

 function subarrs(numSub, numValue, value) { let arr = []; for (let i = 0; i < numSub; i++) { arr.push([]); } arr.forEach(function(sub) { for (let j = 0; j < numValue; j++) { sub.push(value); } }); return arr; } console.log(subarrs(3, 2, 'test')); 

fill will only work on indexes that already exist and its second parameter in your case should be the length of the array (which is the default value) and not length - 1 . fill仅适用于已经存在的索引,在您的情况下,其第二个参数应为数组的length (默认值),而不是length - 1 You can see it here: 在这里你可以看到它:

 let myEmptyArray = []; let myFullArray = [1, 2, 3, 4]; myEmptyArray.fill(0, 0, 4); myFullArray.fill(0, 0, 4); console.log(myEmptyArray, myFullArray) 

You could push an Array with the necessary slots already in place with new Array(numValue) . 您可以使用new Array(numValue) push具有必要插槽的new Array(numValue) Something like this: 像这样:

 function subarrs(numSub, numValue, value) { let arr = []; for (let i = 0; i < numSub; i++) { arr.push(new Array(numValue)); } arr.forEach(function(sub) { sub.fill(value); }); return arr; } console.log(subarrs(3, 2, 'test')); 

You can use .push method to add value into your array instead .fill , see working demo : 您可以使用.push方法向数组中添加值,而不是.fill ,请参见工作演示:

 function subarrs(numSub, numValue, value) { let arr = []; for (let i = 0; i < numSub; i++) { arr.push([]); for (let j = 0; j < numValue; j++) { arr[i].push(value); } } return arr; } console.log(subarrs(3, 2, 'test')); 

If you have es2015+ You can do it easily : 如果您拥有es2015 +,则可以轻松完成:

const subarrs = (length, subLength, value) => 
    [...Array(length)].map(() => [...Array(subLength)].fill(value));

subarrs(3, 2, 'test');

(Edited) after the first comment (编辑)第一条评论之后

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

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