ES6 - Array.fill

此函数使用静态值填充数组中从起始索引到结束索引的所有元素。它返回修改后的数组。

语法

此处给出的语法适用于数组方法 fill(),其中,−

  • value − 用于填充数组的值。

  • start − 这是可选的;起始索引,默认为 0。

  • end − 这是可选的;结束索引,默认为此长度。

arr.fill(value[, start[, end]])

示例

<script>
   //fill
   let nosArr = [10,20,30,40]
   console.log(nosArr.fill(0,1,3))// value ,start,end
   //[10,0,0,40]

   console.log(nosArr.fill(0,1)) // [10,0,0,0]
   console.log(nosArr.fill(0))
</script>

上述代码的输出将如下所示 −

[10, 0, 0, 40]
[10, 0, 0, 0]
[0, 0, 0, 0]