JavaScript:Array.splice 與 Array.slice 如何區分

先看看英文詞典裏的解釋:

splice | BrE splʌɪs, AmE splaɪs |
A.transitive verb
①(join by interweaving the strands) 絞接 jiǎojiē ‹rope(s)›
▸ to splice sth to sth
把某物與某物捻接起來
▸ to splice two things together
絞接兩樣東西
▸ to get spliced
British informal 結婚
②(join at the ends) 粘接 zhānjiē ‹pieces of timber, film, tape›
▸ to splice two things together
把兩樣東西粘接起來

slice B.transitive verb
①(cut into slices) 把…切片 bǎ… qiē piàn ‹bread›
▸ to slice sth thin or thinly/thick or thickly
把某物切成薄片/厚片
▸ to slice sth in two or half
把某物切成兩半
▸ thinly or thin-sliced/thickly or thick-sliced
切成薄片/厚片的
▸ sliced meat/cucumber
肉片/黃瓜片
②(cut from whole) 切下 qiēxia
▸ to slice sth off or from sth;
把某物從某物上切下來
▸ to slice two seconds off the record
把紀錄縮短兩秒鐘
▸ to slice £300 off the budget
把預算減少300英鎊
③(cut through) «knife» 劃破 huápò ‹flesh, cloth›; «fin, wing» 劃過 huáguo ‹water, air›
▸ to slice a leg/an arm to the bone
把腿/胳膊劃破深及骨頭
▸ to slice the water
«ship, bow» 破浪前進
④Sport 削 xiāo ‹ball›
▸ to slice a ball or shot into/to sth
(in tennis) 把球斜切打進/打到某處
▸ to slice a catch to the wicketkeeper
(in cricket) 把球斜切擊出給捕手接住
▸ to slice a corner into the net
(in football) 使角球斜切進網

JavaScript 裏的主要語義

1. splice()方法返回數組中已刪除的項,slice()方法返回數組中的選定元素,作爲新的數組對象。

2. splice()方法更改原始數組,而slice()方法不更改原始數組。

3. splice()方法可以使用n個參數:

參數1:索引,必需。一個整數,指定添加/刪除項目的位置,使用負值指定數組末尾的位置。

參數2:可選。要刪除的項目數。如果設置爲0(零),則不會刪除任何項目。如果沒有通過,將刪除提供的索引中的所有項目。

參數3 ... n:可選。要添加到數組的新項目。

var array=[1,2,3,4,5];
console.log(array.splice(2)); //從index=2的位置截斷,後面的拋棄
// 顯示 [3, 4, 5], 被刪除的項目以新數組形式返回.

console.log(array);
// 顯示 [1, 2], 原數組被修改.

var array2=[6,7,8,9,0];
console.log(array2.splice(2,1)); // 從index=2的位置截斷,後面的拋棄一個
// 顯示  [8]

console.log(array2.splice(2,0));
//顯示空數組 [] , 沒有項目被刪除.

console.log(array2);
// 顯示 [6,7,9,0]

var array3=[11,12,13,14,15];
console.log(array3.splice(2,1,"Hello","World")); //這裏才顯示出絞接的含義!
// 顯示 [13]

console.log(array3);
// 顯示 [11, 12, "Hello", "World", 14, 15]

           -5 -4 -3 -2 -1
            |  |  |  |  |
var array4=[16,17,18,19,20];
             |  |  |  |  |
             0  1  2  3  4

console.log(array4.splice(-2,1,"me"));
// 顯示  [19]

console.log(array4);
// 顯示 [16, 17, 18, "me", 20]

如果Argument(1)是NaN,則將其視爲0。

var array5=[21,22,23,24,25];
console.log(array5.splice(NaN,4,"NaN 被看作是 0"));
// 顯示 [21,22,23,24]

console.log(array5);
// 顯示 ["NaN 被看作是 0",25]

如果Argument(2)小於0或等於NaN,則將其視爲0。

var array6=[26,27,28,29,30];
console.log(array6.splice(2,-5,"Hello"));
// 顯示 []

console.log(array6);
// 顯示 [26,27,"Hello",28,29,30]

console.log(array6.splice(3,NaN,"World"));
// 顯示 []

console.log(array6);
// 顯示 [26,27,"Hello","World",28,29,30]

如果Argument(1)或Argument(2)大於Array的長度,則任一參數都將使用Array的長度。

var array7=[31,32,33,34,35];
console.log(array7.splice(23,3,"Add Me"));
// 顯示 []

console.log(array7);
// 顯示 [31,32,33,34,35,"Add Me"]

console.log(array7.splice(2,34,"Add Me Too"));
// 顯示 [33,34,35,"Add Me"]

console.log(array7);
// 顯示 [31,32,"Add Me Too"]

4. slice()方法可以有2個參數:

參數1:必需。一個整數,指定開始選擇的位置(第一個元素的索引爲0)。使用負數從數組的末尾進行選擇。

參數2:可選。一個整數,指定結束選擇的位置。如果省略,將選擇從數組的起始位置到結尾的所有元素。使用負數從數組的末尾進行選擇。

var array=[1,2,3,4,5]
console.log(array.slice(2));
// 顯示 [3, 4, 5], 返回選定元素.

console.log(array.slice(-2));
// 顯示 [4, 5], 返回選定元素.
console.log(array);
// 顯示 [1, 2, 3, 4, 5], 原數組保持不變.

var array2=[6,7,8,9,0];
console.log(array2.slice(2,4));
// 顯示 [8, 9]

console.log(array2.slice(-2,4));
// 顯示 [9]

console.log(array2.slice(-3,-1));
// 顯示 [8, 9]

console.log(array2);
// 顯示 [6, 7, 8, 9, 0]

如果任一參數是NaN,則將其視爲0。

var array3=[11,12,13,14,15];
console.log(array3.slice(NaN,NaN));
// 顯示 []

console.log(array3.slice(NaN,4));
// 顯示 [11,12,13,14]

console.log(array3);
// 顯示 [11,12,13,14,15]

如果任一參數大於數組的長度,則任一參數都將使用數組的長度

var array4=[16,17,18,19,20];
console.log(array4.slice(23,24));
// 顯示 []

console.log(array4.slice(23,2));
// 顯示 []

console.log(array4.slice(2,23));
// 顯示 [18,19,20]

console.log(array4);
// 顯示 [16,17,18,19,20]

希望有所幫助.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章