in javascript how would I create an empty array of a given size
在javascript中如何创建给定大小的空数组
Psuedo code:
X = 3;
createarray(myarray, X, "");
output:
myarray = ["","",""]
7 个解决方案
#1
23
var arr = new Array(5);
console.log(arr.length) // 5
#2
78
1) To create new array which, you cannot iterate over, you can use array constructor:
1)要创建新的数组,你不能迭代,你可以使用数组构造函数:
Array(100)
or new Array(100)
数组(100)或新数组(100)
2) You can create new array, which can be iterated over like below:
2)你可以创建新的数组,可以迭代如下:
a) All JavaScript versions
a)所有JavaScript版本
- Array.apply:
Array.apply(null, Array(100))
Array.apply:Array.apply(null,Array(100))
b) From ES6 JavaScript version
b)来自ES6 JavaScript版本
- Destructuring operator:
[...Array(100)]
- Array.prototype.fill
Array(100).fill(undefined)
- Array.prototype.from
Array.from({ length: 100 })
解构运算符:[...数组(100)]
Array.prototype.from Array.from({length:100})
You can map over these arrays like below.
您可以映射这些数组,如下所示。
-
Array(4).fill(null).map((u, i) => i)
[0, 1, 2, 3]数组(4).fill(null).map((u,i)=> i)[0,1,2,3]
-
[...Array(4)].map((u, i) => i)
[0, 1, 2, 3][...数组(4)]。map((u,i)=> i)[0,1,2,3]
-
Array.apply(null, Array(4)).map((u, i) => i)
[0, 1, 2, 3]Array.apply(null,Array(4))。map((u,i)=> i)[0,1,2,3]
-
Array.from({ length: 4 }).map((u, i) => i)
[0, 1, 2, 3]Array.from({length:4})。map((u,i)=> i)[0,1,2,3]
#3
12
We use Array.from({length: 500})
in 2017.
我们在2017年使用Array.from({length:500})。
#4
10
Try using while
loop, Array.prototype.push()
尝试使用while循环,Array.prototype.push()
var myArray = [], X = 3;
while (myArray.length < X) {
myArray.push("")
}
Alternatively, using Array.prototype.fill()
或者,使用Array.prototype.fill()
var myArray = Array(3).fill("");
#5
6
If you want an empty array of undefined
elements, you could simply do
如果你想要一个空数组的未定义元素,你可以简单地做
var whatever = new Array(5);
this would give you
这会给你
[undefined, undefined, undefined, undefined, undefined]
and then if you wanted it to be filled with empty strings, you could do
如果你想要用空字符串填充它,你可以做到
whatever.fill('');
which would give you
这会给你
["", "", "", "", ""]
And if you want to do it in one line:
如果你想在一行中做到这一点:
var whatever = Array(5).fill('');
#6
4
In 2018 and thenceforth we shall use [...Array(500)]
to that end.
在2018年,从那时起我们将[... Array(500)]用于此目的。
#7
-2
I would do this:
我会这样做:
function emptyArray(length){
var a = [];
for(var i=0,l=length; i<l; i++){
a.push('');
}
return a;
}
var empty10 = emptyArray(10);
if you want empty Strings, otherwise
如果你想要空字符串,否则
var empty10 = new Array(10);
will make a truly empty Array.
将制作一个真正空的数组。
#1
23
var arr = new Array(5);
console.log(arr.length) // 5
#2
78
1) To create new array which, you cannot iterate over, you can use array constructor:
1)要创建新的数组,你不能迭代,你可以使用数组构造函数:
Array(100)
or new Array(100)
数组(100)或新数组(100)
2) You can create new array, which can be iterated over like below:
2)你可以创建新的数组,可以迭代如下:
a) All JavaScript versions
a)所有JavaScript版本
- Array.apply:
Array.apply(null, Array(100))
Array.apply:Array.apply(null,Array(100))
b) From ES6 JavaScript version
b)来自ES6 JavaScript版本
- Destructuring operator:
[...Array(100)]
- Array.prototype.fill
Array(100).fill(undefined)
- Array.prototype.from
Array.from({ length: 100 })
解构运算符:[...数组(100)]
Array.prototype.from Array.from({length:100})
You can map over these arrays like below.
您可以映射这些数组,如下所示。
-
Array(4).fill(null).map((u, i) => i)
[0, 1, 2, 3]数组(4).fill(null).map((u,i)=> i)[0,1,2,3]
-
[...Array(4)].map((u, i) => i)
[0, 1, 2, 3][...数组(4)]。map((u,i)=> i)[0,1,2,3]
-
Array.apply(null, Array(4)).map((u, i) => i)
[0, 1, 2, 3]Array.apply(null,Array(4))。map((u,i)=> i)[0,1,2,3]
-
Array.from({ length: 4 }).map((u, i) => i)
[0, 1, 2, 3]Array.from({length:4})。map((u,i)=> i)[0,1,2,3]
#3
12
We use Array.from({length: 500})
in 2017.
我们在2017年使用Array.from({length:500})。
#4
10
Try using while
loop, Array.prototype.push()
尝试使用while循环,Array.prototype.push()
var myArray = [], X = 3;
while (myArray.length < X) {
myArray.push("")
}
Alternatively, using Array.prototype.fill()
或者,使用Array.prototype.fill()
var myArray = Array(3).fill("");
#5
6
If you want an empty array of undefined
elements, you could simply do
如果你想要一个空数组的未定义元素,你可以简单地做
var whatever = new Array(5);
this would give you
这会给你
[undefined, undefined, undefined, undefined, undefined]
and then if you wanted it to be filled with empty strings, you could do
如果你想要用空字符串填充它,你可以做到
whatever.fill('');
which would give you
这会给你
["", "", "", "", ""]
And if you want to do it in one line:
如果你想在一行中做到这一点:
var whatever = Array(5).fill('');
#6
4
In 2018 and thenceforth we shall use [...Array(500)]
to that end.
在2018年,从那时起我们将[... Array(500)]用于此目的。
#7
-2
I would do this:
我会这样做:
function emptyArray(length){
var a = [];
for(var i=0,l=length; i<l; i++){
a.push('');
}
return a;
}
var empty10 = emptyArray(10);
if you want empty Strings, otherwise
如果你想要空字符串,否则
var empty10 = new Array(10);
will make a truly empty Array.
将制作一个真正空的数组。