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

const array = [1, 2, 3, 4, 5];
array.splice(index, 1);
array.slice(index, 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 ()

其他回答

按索引拼接和删除数组项

指数= 2

//splice & will modify the origin array
const arr1 = [1,2,3,4,5];
//slice & won't modify the origin array
const arr2 = [1,2,3,4,5]

console.log("----before-----");
console.log(arr1.splice(2, 1));
console.log(arr2.slice(2, 1));

console.log("----after-----");
console.log(arr1);
console.log(arr2);

Let log = console.log; //splice &将修改原始数组 Const arr1 = [1,2,3,4,5]; //slice &不会修改原始数组 Const arr2 = [1,2,3,4,5] 日志(前”————“); 日志(arr1。接头(2,1)); 日志(arr2。片(2,1)); 日志(”——后——”); 日志(arr1); 日志(arr2);

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 ()

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.

切片和拼接紧密相连,但用途却截然不同:

slice函数用于选择数组的一部分。它的目的是返回值。它的执行并不影响它的主题。

splice函数用于从数组中删除元素。它的目的是修饰它的主题。如果需要,它仍然返回已删除项的副本,以供参考。

Splice和Slice是内置的Javascript命令,而不是特定的AngularJS命令。Slice返回从“start”到“end”说明符之前的数组元素。Splice改变实际的数组,从“start”开始,并保持指定的元素数量。谷歌有很多这方面的信息,只要搜索。