Javascript 默认数组值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2044760/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
default array values
提问by Derek Adair
Is there a way to assign a default values to arrays in javascript?
有没有办法在javascript中为数组分配默认值?
ex: an array with 24 slots that defaults to 0
前任: an array with 24 slots that defaults to 0
采纳答案by kennebec
Array.prototype.repeat= function(what, L){
while(L) this[--L]= what;
return this;
}
var A= [].repeat(0, 24);
var A= [].repeat(0, 24);
alert(A)
警报(一)
回答by deadrunk
var a = Array.apply(null, Array(24)).map(function() { return 0 });
// or:
var a = Array.apply(null, Array(5)).map(Boolean).map(Number);
回答by Aik
You can use the fillfunction on an array:
您可以fill在数组上使用该函数:
Array(24).fill(0)
Note:fillwas only introduced in ECMAScript 2015 (alias "6"), so as of 2017 browser support is still very limited (for example no Internet Explorer).
注意:fill仅在 ECMAScript 2015 中引入(别名“6”),因此截至 2017 年浏览器支持仍然非常有限(例如没有 Internet Explorer)。
回答by tvanfosson
A little wordy, but it works.
有点罗嗦,但它有效。
var aray = [ 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0 ];
回答by Vito Gentile
If you use a "very modern" browser (check the compatibility table), you can rely on fill:
如果您使用“非常现代”的浏览器(检查兼容性表),您可以依赖fill:
var array = new Array(LENGTH);
array.fill(DEFAULT_VALUE);
For several reasons, the use of new Array()is not appreciated by a lot of developers; so you can do the same as follows:
出于多种原因,new Array()很多开发人员不喜欢使用;所以你可以做同样的事情:
var array = [];
for(var i=0; i<LENGTH; ++i) array.push(DEFAULT_VALUE);
The latter is much more compatible, and as you can see both solutions are represented by two lines of code.
后者更加兼容,正如您所见,这两种解决方案都由两行代码表示。
回答by sajad saderi
the best and fastest solution is :
最好和最快的解决方案是:
Array(length of the array).fill(a default value you want put in the array)
example
例子
Array(5).fill(1)
and result will be
结果将是
[1,1,1,1,1]
you can put any thing you like in it:
你可以放任何你喜欢的东西:
Array(5).fill({name : ""})
Now if you want to change some of the current value in the array you can use
现在,如果您想更改数组中的某些当前值,您可以使用
[the created array ].fill(a new value , start position, end position)
like
喜欢
[1,1,1,1,1].fill("a",1,3)
and output is
和输出是
[1, "a", "a", 1, 1]
回答by Oscar Rieken
If you are using ES6 and up you can use
如果您使用的是 ES6 及更高版本,则可以使用
new Array(24).fill(0);
this also works:
这也有效:
Array.from({ length: 24}, () => 0)
回答by Thomas Eding
I personally use:
我个人使用:
function replicate (n, x) {
var xs = [];
for (var i = 0; i < n; ++i) {
xs.push (x);
}
return xs;
}
Example:
例子:
var hellos = replicate (100, "hello");
回答by kennytm
No.
不。
You have to use a loop.
你必须使用一个循环。
var a = new Array(24);
for (var i = a.length-1; i >= 0; -- i) a[i] = 0;
回答by Tomá?
And now, more cleverly :)
@see JavaScript Array reference
Example on JSFiddle
现在,更聪明的是 :)
@see JavaScript Array reference
Example on JSFiddle
var len = 100;
var ch = '0';
alert ( new Array(len).join( ch ) );

