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]

希望有所帮助.

你可能感兴趣的:(数组,javascript)