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]
广告
© . All rights reserved.