js数组方法Array.fill

Array.fill

arr.fill(value[, start[, end]])
fill() 方法用一个固定值填充一个数组中,
从起始索引到终止索引内的全部元素,
不包括终止索引,
返回被修改后的数组。

value:用来填充数组元素的值。
start:起始索引,默认值为0。
end:终止索引,默认值为 this.length。

// 填充数组
const arr = Array(3).fill(1);  // [1, 1, 1]
const arr1 = [1,2,3,4,5].fill(0,1,3); // [1, 0, 0, 4, 5]

// 填充类数组
const obj = [].fill.call({ length: 3 }, 4);  // {0: 4, 1: 4, 2: 4, length: 3}

你可能感兴趣的:(ES标准,javascript,es6)