Js array problem

currently encountered a problem:

it is known that the length of the array is 5, there may be one, two or more values in the array, and the rest of the values want to be filled with "0".
in addition to the methods of the for loop, js has no native methods for arrays to do this.

Oct.14,2021

var arr = new Array(20);
arr[2] = 1;
arr[18] =2 ;
var r = Array.from(arr,(elem) => elem = elem === undefined ? 0 : elem);

try to see if it's what you want

ES5

var arr = new Array(20);
arr[2] = 1;
arr[18] =2 ;
arr = JSON.parse(JSON.stringify(arr)).map(function (elem) {
  return elem = elem === null ? 0 : elem
});
console.log(arr);

look at

Menu