看了一下mdn中的對fill()解釋和語法

代碼參考:

var array1 = [1, 2, 3, 4];

// fill with 0 from position 2 until position 4

console.log(array1.fill(0, 2, 4));//0表示數組元素轉換的值,2表示從數組索引爲2的位置開始,4表示數組長度爲4

// expected output: [1, 2, 0, 0]

// fill with 5 from position 1

console.log(array1.fill(5, 1));//5表示數組後面轉換的元素值爲5,1表示從數組索引爲1的位置開始,後面的元素都轉換爲5

// expected output: [1, 5, 5, 5]

console.log(array1.fill(6));//6表示數組需要轉換的值是6,因爲這邊沒有些元素轉換的索引位置,所以默認數組的所有元素都是6

// expected output: [6, 6, 6, 6]

如果我的理解有錯,希望大家可以指出來。。。謝謝啦。我也是跟着自己的理解寫出來的,可能我的理解會有些錯。

查看原文 >>
相關文章