A very simple for-loop is all you need. There's no fill-function in JavaScript.
var length = arr.length,
for (var i = 0; i < length; i++) {
arr[i] = 0;
}
문제
I have an array and I want to overwrite all values in it (that are all numbers) with a 0. So the array already exists.
I can do this with a for loop. However, is there a fill() call of some kind like the Java Arrays.fill()?
해결책
A very simple for-loop is all you need. There's no fill-function in JavaScript.
var length = arr.length,
for (var i = 0; i < length; i++) {
arr[i] = 0;
}
다른 팁
You can use map for this.
var arr = [1,2,3];
arr = arr.map(function() {return 0});
//arr = [0, 0, 0]
The performance may be worse than a plain for loop.