2025年5月26日 星期一

再探 Array.copyWithin() 之四

 再探 Array.copyWithin() 之四

前言

    在之前的 再探 Array.copyWithin() 之三 介紹當 start 的值是負值的狀況,這次來學習 end的值是負值。


內容

  範例如下

let ar = [ 0 , 1 , 2 , 3 , 4 , 5 ];
//
console.log( [...ar].copyWithin( 2 , 0 , -4 ) );//[0, 1, 0, 1, 4, 5]
console.log( [...ar].copyWithin( 2 , 0 , -3 ) );//[0, 1, 0, 1, 2, 5]
console.log( [...ar].copyWithin( 0 , 0 , -1 ) );//[0, 1, 2, 3, 4, 5]


當引數 end 是負值,代表複製的結尾索引是從結尾來算。第一例的複製結尾是 -4 ,從結尾來算可以得到索引 2 ,所以第一例的結果跟直接將 end 填上 2 的結果一樣,第二例的 end 是 -3 ,換算回正索引的話是 3 ,所以結果和 end 是 3 的狀況一樣。最後一例是將 end 填上 -1 ,也就是陣列的最後一個索引,這其實就何不填 end 時的結果一樣是複製到結尾。


參考資料

[ developer.mozilla.org ] Array.prototype.copyWithin()


相關文章與資料

再探 Array.copyWithin() 之三

再探 Array.copyWithin() 之二

再探 Array.copyWithin()

初探 Array.copyWithin()

2025年5月19日 星期一

再探 Array.copyWithin() 之三

 再探 Array.copyWithin() 之三

前言

  在之前的 再探 Array.copyWithin() 之二 介紹當 target 的值是負值的狀況,這次來學習 start 的值是負值。


內容

  範例如下

let ar = [ 0 , 1 , 2 , 3 , 4 , 5 ];
//
console.log( [...ar].copyWithin( 2 , -1 ) );//[0, 1, 5, 3, 4, 5]
console.log( [...ar].copyWithin( 2 , -3 ) );//[0, 1, 3, 4, 5, 5]
console.log( [...ar].copyWithin( 0 , -6 ) );//[0, 1, 2, 3, 4, 5]


當引數 start 是負值,代表複製的開頭索引是從結尾來算,第一例是 -1 ,所以最後只複製了一個,第二例是 -3 ,也就是複製倒數的三個,要注意順序依舊是 3,4,5 ,而不是 5,4,3。當沒有引數 end 的情況下。索引是負值可以解釋為要複製多少數量,所以當它是負的陣列大小時,等於複製全部陣列。


參考資料

[ developer.mozilla.org ] Array.prototype.copyWithin()


相關文章與資料

再探 Array.copyWithin() 之二

再探 Array.copyWithin()

初探 Array.copyWithin()

2025年5月12日 星期一

再探 Array.copyWithin() 之二

 再探 Array.copyWithin() 之二

前言

  在之前的 再探 Array.copyWithin() 介紹三個參數的用法,但查閱 [ developer.mozilla.org ] Array.prototype.copyWithin() 發現三個引數其實還可以輸入負值!這次先來學習引數 target 是負值的狀況。


內容

  範例如下

let ar = [ 0 , 1 , 2 , 3 , 4 , 5 ];
//
console.log( [...ar].copyWithin( -1 , 0 ) );//[0, 1, 2, 3, 4, 0]
console.log( [...ar].copyWithin( -2 , 0 ) );//[0, 1, 2, 3, 0, 1]
console.log( [...ar].copyWithin( -6 , 0 ) );//[0, 1, 2, 3, 4, 5]


當引數 target 是負值時,代表的是從陣列尾端倒過來指定,可以看到範例1 與 範例2 的結果是倒過來指定複製的位址,如果大於負陣列長度時,也就是範例3,這樣的結果就等於無作用,因為已經過了開頭了!


參考資料

[ developer.mozilla.org ] Array.prototype.copyWithin()


相關文章與資料

再探 Array.copyWithin()

初探 Array.copyWithin()

2025年5月5日 星期一

再探 Array.copyWithin()

 再探 Array.copyWithin()

前言

  在先前的 初探 Array.copyWithin() 裡基本的使用了 Array.copyWithin() 。範例使用 Array.copyWithin() 時都使用兩個引數,但 Array.copyWithin()  還可以使用三個引數,在此把學習的過程做個紀錄。


內容

  Array.copyWithin() 使用三個引數時會比兩個引數多出一個 end 的引數,該引數放在最後,配合第二個引數 start 來使用,start 表示要從來源 Array 的哪個索引開始複製, end 表示複製到來源 Array的那個索引為止,使用的範例如下

let ar = [ 0 , 1 , 2 , 3 , 4 , 5 ];
//
console.log( [...ar].copyWithin( 1 , 0 ) );//[0, 0, 1, 2, 3, 4]
console.log( [...ar].copyWithin( 1 , 0 , ar.length) );//[0, 0, 1, 2, 3, 4]
console.log( [...ar].copyWithin( 1 , 0 , 3) );//[0, 0, 1, 2, 4, 5]
console.log( [...ar].copyWithin( 3 , 1 , 2) );//[0, 1, 2, 1, 4, 5]
console.log( [...ar].copyWithin( 3 , 1 , 1) );//[0, 1, 2, 3, 4, 5]


end 控制的是要複製到的索引,所以如果 end 填上陣列的數量的結果就跟只使用兩個引數的結果一樣,可以在範例的第一例與第二例看到。第三例有設置 end 為 3 ,所以結果看到索引 1~3 是被限制的 copy ,索引 4~5 則保持原來的值。要注意的是 end 並不是表示數量,也和 start 一樣是索引,第四例的 start 與 end 只差 1 ,所以結果只改變了索引 3 ,如果把 start 與 end 填成一樣的,就會發現不會有任何的值被改變,就像第五例那樣。


參考資料

[ developer.mozilla.org ] Array.prototype.copyWithin()


相關文章與資料

初探 Array.copyWithin()