拼接和切片的区别是什么?
const array = [1, 2, 3, 4, 5];
array.splice(index, 1);
array.slice(index, 1);
拼接和切片的区别是什么?
const array = [1, 2, 3, 4, 5];
array.splice(index, 1);
array.slice(index, 1);
当前回答
有3个不同点:
Splice将从原始数组中删除所选元素,并将它们作为一个新数组返回-请注意,原始数组将不再拥有它们-。Slice将使用所选元素创建一个新数组,而不会影响原来的数组。 Splice接收起始索引和从该点移除多少元素作为参数。Slice接收2个索引,开始和结束。 Splice可用于通过传递可选参数在数组中的特定位置添加元素。
其他回答
splice()方法返回数组中已删除的项。 slice()方法将数组中所选的元素作为一个新的数组对象返回。
splice()方法改变原始数组,slice()方法不改变原始数组。
Splice() method can take n number of arguments: Argument 1: Index, Required. Argument 2: Optional. The number of items to be removed. If set to 0(zero), no items will be removed. And if not passed, all item(s) from provided index will be removed. Argument 3..n: Optional. The new item(s) to be added to the array. slice() method can take 2 arguments: Argument 1: Required. An integer that specifies where to start the selection (The first element has an index of 0). Use negative numbers to select from the end of an array. Argument 2: Optional. An integer that specifies where to end the selection. If omitted, all elements from the start position and to the end of the array will be selected. Use negative numbers to select from the end of an array.
Splice和Slice都是Javascript数组函数。
拼接与切片
splice()方法返回数组中被删除的元素,slice()方法返回数组中被选择的元素,作为一个新的数组对象。 splice()方法改变原始数组,slice()方法不改变原始数组。 splice()方法可以接受n个参数,slice()方法可以接受2个参数。
实例拼接
参数1:索引,必需的。指定在什么位置添加/删除项的整数。使用负值指定从数组末尾开始的位置。
论据2:可选。要删除的项的数目。如果设置为0(零),则不会删除任何项。如果没有通过,则从所提供的索引中删除所有项。
参数3…n:可选。要添加到数组中的新项。
数组var =[1、2、3、4、5); console.log (array.splice (2)); //显示[3,4,5],作为一个新的数组对象返回已删除的项。 console.log(数组); //显示[1,2],原始数组改变。 var array2 = [6 7 8 9 0]; console.log (array2.splice (2,1)); //显示[8] console.log (array2.splice (2,0)); //显示[],表示没有删除任何项。 console.log (array2); //显示[6,7,9,0]
切片示例
论据1:必须的。指定从何处开始选择的整数(第一个元素的索引为0)。使用负数从数组的末尾开始选择。
论据2:可选。一个整数,指定在何处结束选择,但不包括。如果省略,将选择从数组开始位置到数组结束的所有元素。使用负数从数组的末尾进行选择。
var array=[1,2,3,4,5] console.log(array.slice(2)); // shows [3, 4, 5], returned selected element(s). console.log(array.slice(-2)); // shows [4, 5], returned selected element(s). console.log(array); // shows [1, 2, 3, 4, 5], original array remains intact. var array2=[6,7,8,9,0]; console.log(array2.slice(2,4)); // shows [8, 9] console.log(array2.slice(-2,4)); // shows [9] console.log(array2.slice(-3,-1)); // shows [8, 9] console.log(array2); // shows [6, 7, 8, 9, 0]
拼接- MDN参考- ECMA-262规范
语法 数组中。拼接(item1[[开始,deleteCount[第二条 [, ...]]]])
参数
开始:必需的。初始索引。 如果start为负,则将其视为“Math.max((数组。长度+ start), 0)”,按照规范(下面提供的示例)有效地从数组的末尾开始。 deleteCount:可选的。要删除的元素数量(如果没有提供,则全部从开始删除)。 Item1, item2,…:可选的。要从起始索引添加到数组的元素。
返回:删除元素的数组(如果没有删除则为空数组)
改变原始数组:是的
例子:
Const数组= [1,2,3,4,5]; //删除第一个元素 console.log('已删除的元素:',数组。Splice(0,1), '突变数组:',数组); //元素删除:[1]mutated array: [2,3,4,5] // array = [2,3,4,5] //删除最后一个元素start ->数组。长度+起始= 3) console.log('已删除的元素:',数组。Splice(- 1,1), '突变数组:',数组); //元素删除:[5]mutated array: [2,3,4]
更多的例子在MDN拼接的例子
切片- MDN参考- ECMA-262规范
语法 数组中。片([[,]]开始) 参数
开始:可选的。初始索引(默认为0)。 如果begin为负数,则将其处理为“Math.max((数组。长度+ begin), 0)”按照规范(下面提供的示例)有效地从数组的末尾开始。 结束:可选的。抽取的最后一个索引,但不包括(default array.length)。如果end为负数,则将其处理为“Math.max((数组。长度+ begin),0)”按照规范(下面提供的示例)有效地从数组的末尾开始。
返回:包含已提取元素的数组。
突变原:没有
例子:
Const数组= [1,2,3,4,5]; //提取第一个元素 console.log('提取的元素:',数组。Slice (0,1), 'array:', array); //元素提取:[1]array: [1,2,3,4,5] //提取最后一个元素start ->数组。长度+起始= 4) console.log('提取的元素:',array.slice(-1), 'array:', array); //元素提取:[5]array: [1,2,3,4,5]
更多的例子在MDN切片的例子
性能比较
不要认为这是绝对的真理,因为根据不同的场景,一个可能比另一个表现更好。 性能测试
Slice()和Splice() javascript内置函数之间的区别是, Slice返回删除的项,但没有改变原始数组; 就像,
// (original Array)
let array=[1,2,3,4,5]
let index= array.indexOf(4)
// index=3
let result=array.slice(index)
// result=4
// after slicing=> array =[1,2,3,4,5] (same as original array)
但在splice()情况下,它会影响原始数组;就像,
// (original Array)
let array=[1,2,3,4,5]
let index= array.indexOf(4)
// index=3
let result=array.splice(index)
// result=[4,5]
// after splicing array =[1,2,3] (splicing affects original array)
这里有一个简单的技巧来记住切片和拼接之间的区别
var a=['j','u','r','g','e','n'];
// array.slice(startIndex, endIndex)
a.slice(2,3);
// => ["r"]
//array.splice(startIndex, deleteCount)
a.splice(2,3);
// => ["r","g","e"]
要记住的技巧:
将“spl”(splice的前3个字母)视为“指定长度”的缩写,即第二个参数应该是长度而不是索引