javascript 切片,拼接和扩散之间的差异

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了javascript 切片,拼接和扩散之间的差异相关的知识,希望对你有一定的参考价值。

1st Argument: Specifies from where the selection should be started

2nd Argument: Specifies at which level the endpoint should be. If you didn’t put this in the parenthesis 
while calling the slice method, it will return the elements from the starting index to the end of the array.

Note: 
Slice always returns the selected elements from the array.
Slice won’t change the array. The array remains intact

var arr1 = [1,5,8,9];
arr1.slice(1); // [5,8,9]

arr1.slice(1,3); //[ 5, 8 ]

arr1.slice(-2);//[ 8, 9 ] 
1st Argument: Specifies at which position a new element or existing element should be added/removed. If the 
value is negative the position will be counted from the end of the array.

2nd Argument: The numbers of elements to be removed from the starting position. If it is 0, then no elements 
will be removed. If it is not passed, it will delete all elements from the starting position.

3rd Argument ->nth Argument: The value of the items you want to add to the array.

var arr1 = [1,5,8,9];

arr1.splice(1,2);// [ 5, 8 ] 

arr1.splice(1,2,'Hi','Medium')

Note:
Splice will return removed elements from the array only.
Splice will change the original array
var arr1 = [1,3,6,7];
var arr2 = [5,arr1,8,9]; // [ 5, [ 1, 3, 6, 7 ], 8, 9 ]

Definition: Allows an iterable such as an array expression or string to be expanded in places where zero or 
more arguments (for function calls) or elements (for array literals) are expected, or an object expression to
be expanded in places where zero or more key-value pairs (for object literals) are expected.

var arr2 = [5,...arr1,8,9]; //[ 5, 1, 3, 6, 7, 8, 9 ]

//Copy an array
var arr = [1, 2, 3];
var arr2 = [...arr]; // like arr.slice()

以上是关于javascript 切片,拼接和扩散之间的差异的主要内容,如果未能解决你的问题,请参考以下文章

运算符的差异

javascript 了解import和require之间的差异

Javascript和datediff mysql中日期之间的日期差异不相等

移动 iOS 和 Android Safari/Chrome 之间的 JavaScript 差异

如何使用javascript查找两个日期之间的天数差异

javascript 探索var和let关键字之间的差异