拼接和切片的区别是什么?

const array = [1, 2, 3, 4, 5];
array.splice(index, 1);
array.slice(index, 1);

当前回答

另一个例子:

[2,4,8].splice(1, 2) -> returns [4, 8], original array is [2]

[2,4,8].slice(1, 2) -> returns 4, original array is [2,4,8]

其他回答

Splice()改变原始数组,而slice()不会,但它们都返回数组对象。

请看下面的例子:

var array=[1,2,3,4,5];
console.log(array.splice(2));

这将返回[3,4,5]。原始数组受到影响,导致数组为[1,2]。

var array=[1,2,3,4,5]
console.log(array.slice(2));

这将返回[3,4,5]。原始数组不受影响,导致数组为[1,2,3,4,5]。

下面是简单的小提琴,它证实了这一点:

/ /拼接 数组var =[1、2、3、4、5); console.log (array.splice (2)); / /片 var array2 =[1、2、3、4、5) console.log (array2.slice (2)); console.log(“——后——”); console.log(数组); console.log (array2);

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]

切片不改变原始数组,它返回新的数组,但拼接改变原始数组。

example: var arr = [1,2,3,4,5,6,7,8];
         arr.slice(1,3); // output [2,3] and original array remain same.
         arr.splice(1,3); // output [2,3,4] and original array changed to [1,5,6,7,8].

拼接方法的第二个参数不同于切片方法。在拼接中,第二个参数表示要删除的元素的计数,在切片中,它表示结束索引。

arr.splice(-3,-1); // output [] second argument value should be greater then 
0.
arr.splice(-3,-1); // output [6,7] index in minus represent start from last.

-1代表最后一个元素,从-3到-1。 以上是拼接法与切片法的主要区别。

slice()方法复制数组的给定部分,并将复制的部分作为一个新数组返回。它不会改变原始数组。

splice()方法通过向数组中添加或删除元素来更改数组。

下面是切片语法:

array.slice(from, until);

// example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.slice(1, 3)
console.log({array, newArray})

// output: array: [1, 2, 3, 4, 5, 6], newArray: [2, 3]

注意:Slice()方法也可以用于字符串。

下面是拼接的语法:

//For removing elements, we need to give the index parameter,
// and the number of elements to be removed

array.splice(index, number of elements to be removed);


//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice(1, 3)
console.log({array, newArray})

// output: array: [1, 5, 6], newArray: [2, 3, 4]

注意:如果不定义第二个形参,则从给定索引开始的每个元素都将从数组中删除

// For adding elements, we need to give them as the 3rd, 4th, ... parameter
array.splice(index, number of elements to be removed, element, element);

//example
let array = [1, 2, 3, 4, 5, 6]
let newArray = array.splice(1, 3, 'a', 'b')
console.log({array, newArray})

// output: array: [1, ,'a', 'b', 5, 6], newArray: [2, 3, 4]

相关链接:

让我们来澄清JavaScript中slice()、splice()和split()方法的混淆

Array.prototype.slice ()

Array.prototype.splice ()

S LICE =给出数组的一部分&没有分裂原始数组

SP LICE =给出数组的一部分&分裂原始数组

我个人觉得这更容易记住,因为这两个术语总是让我这个网页开发初学者感到困惑。