Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 使用arrayLength在typescript中创建二维空数组_Arrays_Typescript - Fatal编程技术网

Arrays 使用arrayLength在typescript中创建二维空数组

Arrays 使用arrayLength在typescript中创建二维空数组,arrays,typescript,Arrays,Typescript,使用typescript,我想初始化一个具有特定长度和数字类型的2D空数组。 我们可以创建如下1D阵列: const test: Array<number> = new Array(5); twoArray = new Array(new Array(5), new Array(5)); 这将创建一个2D阵列5x10。 我想在typescript中执行类似的操作。尝试创建如下多维数组: const test: Array<number> = new Array(

使用typescript,我想初始化一个具有特定长度和数字类型的2D空数组。 我们可以创建如下1D阵列:

 const test: Array<number> = new Array(5);
twoArray = new Array(new Array(5), new  Array(5));
这将创建一个2D阵列5x10。
我想在typescript中执行类似的操作。

尝试创建如下多维数组:

 const test: Array<number> = new Array(5);
twoArray = new Array(new Array(5), new  Array(5));

没有循环你是做不到的。但是,获取二维数组的最简单方法是使用-创建具有所需长度的数组,并为其每个成员生成新数组:

const test=Array.from({length:5},()=>Array.from({length:10}));

控制台日志(测试)没有循环就无法完成(某种程度上,有些方法会在内部循环)。但是您真的需要用一些长度初始化数组吗?因为
newarray(5)
几乎什么都不做,所以它只设置数组的
length
属性,就是这样。它不会为数组或任何东西保留空间,您可以随时添加更多内容。此外,您甚至不能映射或使用任何类似的数组方法,因为所有这些都是被跳过的空插槽。是的,我需要它,因为在此之后,我必须访问元素测试[2][5],然后访问测试[1][3]。。。没有具体的订单