Code
码
var cool = new Array(3);
cool[setAll] = 42; //cool[setAll] is just a pseudo selector..
alert(cool);
Result
结果
A alert message:
提醒消息:
42,42,42
How do I change/set all values of an array to a specific value?
如何将数组的所有值更改/设置为特定值?
7 个解决方案
#1
38
There's no built-in way, you'll have to loop over all of them:
没有内置方式,你必须遍历所有这些:
function setAll(a, v) {
var i, n = a.length;
for (i = 0; i < n; ++i) {
a[i] = v;
}
}
http://jsfiddle.net/alnitak/xG88A/
http://jsfiddle.net/alnitak/xG88A/
If you really want, do this:
如果你真的想要,请这样做:
Array.prototype.setAll = function(v) {
var i, n = this.length;
for (i = 0; i < n; ++i) {
this[i] = v;
}
};
and then you could actually do cool.setAll(42)
(see http://jsfiddle.net/alnitak/ee3hb/).
然后你实际上可以做cool.setAll(42)(参见http://jsfiddle.net/alnitak/ee3hb/)。
Some people frown upon extending the prototype of built-in types, though.
不过,有些人不屑于扩展内置类型的原型。
EDIT ES5 introduced a way to safely extend both Object.prototype
and Array.prototype
without breaking for ... in ...
enumeration:
编辑ES5引入了一种安全地扩展Object.prototype和Array.prototype的方法,而不会破坏... in ...枚举:
Object.defineProperty(Array.prototype, 'setAll', {
value: function(v) {
...
}
});
EDIT 2 In ES6 draft there's also now Array.prototype.fill
, usage cool.fill(42)
编辑2在ES6草案中,现在还有Array.prototype.fill,用法cool.fill(42)
#2
17
map
is the most logical solution for this problem.
map是解决此问题的最合理的解决方案。
let xs = [1, 2, 3];
xs = xs.map(x => 42);
xs // -> [42, 42, 42]
However, if there is a chance that the array is sparse, you'll need to use for
or, even better, for .. of
.
但是,如果阵列有可能是稀疏的,那么你需要使用for或者甚至更好的。
See:
看到:
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
#3
14
The ES6 approach is very clean. So first you initialize an array of x length, and then call the fill
method on it.
ES6的方法很干净。首先,初始化一个x长度的数组,然后在其上调用fill方法。
let arr = new Array(3).fill(9)
this will create an array with 3 elements like:
这将创建一个包含3个元素的数组,如:
[9, 9, 9]
#4
3
Use a for
loop and set each one in turn.
使用for循环并依次设置每个循环。
#5
1
The other answers are Ok, but a while loop seems more appropriate:
其他答案都是好的,但是一个while循环似乎更合适:
function setAll(array, value) {
var i = array.length;
while (i--) {
array[i] = value;
}
}
A more creative version:
更具创意的版本:
function replaceAll(array, value) {
var re = new RegExp(value, 'g');
return new Array(++array.length).toString().replace(/,/g, value).match(re);
}
May not work everywhere though. :-)
虽然可能无处不在。 :-)
#6
0
One solution using native code is this:
使用本机代码的一个解决方案是:
var cool = new Array(3);
cool.fill(42);
#7
-1
Try this:
尝试这个:
for(var i in array) array[i]=fixedValue;
#1
38
There's no built-in way, you'll have to loop over all of them:
没有内置方式,你必须遍历所有这些:
function setAll(a, v) {
var i, n = a.length;
for (i = 0; i < n; ++i) {
a[i] = v;
}
}
http://jsfiddle.net/alnitak/xG88A/
http://jsfiddle.net/alnitak/xG88A/
If you really want, do this:
如果你真的想要,请这样做:
Array.prototype.setAll = function(v) {
var i, n = this.length;
for (i = 0; i < n; ++i) {
this[i] = v;
}
};
and then you could actually do cool.setAll(42)
(see http://jsfiddle.net/alnitak/ee3hb/).
然后你实际上可以做cool.setAll(42)(参见http://jsfiddle.net/alnitak/ee3hb/)。
Some people frown upon extending the prototype of built-in types, though.
不过,有些人不屑于扩展内置类型的原型。
EDIT ES5 introduced a way to safely extend both Object.prototype
and Array.prototype
without breaking for ... in ...
enumeration:
编辑ES5引入了一种安全地扩展Object.prototype和Array.prototype的方法,而不会破坏... in ...枚举:
Object.defineProperty(Array.prototype, 'setAll', {
value: function(v) {
...
}
});
EDIT 2 In ES6 draft there's also now Array.prototype.fill
, usage cool.fill(42)
编辑2在ES6草案中,现在还有Array.prototype.fill,用法cool.fill(42)
#2
17
map
is the most logical solution for this problem.
map是解决此问题的最合理的解决方案。
let xs = [1, 2, 3];
xs = xs.map(x => 42);
xs // -> [42, 42, 42]
However, if there is a chance that the array is sparse, you'll need to use for
or, even better, for .. of
.
但是,如果阵列有可能是稀疏的,那么你需要使用for或者甚至更好的。
See:
看到:
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
- https://developer.mozilla.org/en-US/docs/JavaScript/Reference/Global_Objects/Array/map
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
- https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of
#3
14
The ES6 approach is very clean. So first you initialize an array of x length, and then call the fill
method on it.
ES6的方法很干净。首先,初始化一个x长度的数组,然后在其上调用fill方法。
let arr = new Array(3).fill(9)
this will create an array with 3 elements like:
这将创建一个包含3个元素的数组,如:
[9, 9, 9]
#4
3
Use a for
loop and set each one in turn.
使用for循环并依次设置每个循环。
#5
1
The other answers are Ok, but a while loop seems more appropriate:
其他答案都是好的,但是一个while循环似乎更合适:
function setAll(array, value) {
var i = array.length;
while (i--) {
array[i] = value;
}
}
A more creative version:
更具创意的版本:
function replaceAll(array, value) {
var re = new RegExp(value, 'g');
return new Array(++array.length).toString().replace(/,/g, value).match(re);
}
May not work everywhere though. :-)
虽然可能无处不在。 :-)
#6
0
One solution using native code is this:
使用本机代码的一个解决方案是:
var cool = new Array(3);
cool.fill(42);
#7
-1
Try this:
尝试这个:
for(var i in array) array[i]=fixedValue;