Difference method and splice arrays slice method

A, slice () method

slice () method returns the selected elements from the existing array.

grammar:

arrayObject.slice(start,end)

parameter:

       Start: (cutout start position of the index, including the start index) must choose where to start working. If it is negative, then it provides an array of tail begin to run from the location. (-1 is the last element, -2 means penultimate element, and so on).

       End: (end position taken index, ending index is not included) Alternatively, where the end of a predetermined selection. This index is an array of fragments at the end of the array, if the parameter is not specified, then sliced array contains all of the elements from the start (at the beginning) to the end of the array. If this parameter is negative, then it is prescribed begin to run from the end of the array elements.

return value:

Returns a new array, comprising an array of objects from the start to the end (the end that does not contain the element) of the element (ArrayObject) in.

Note :

1, slice method does not modify the array, but will return a sub-array. If you want to delete some elements should use Array.splice ()

2, negative values ​​may be used to select elements from the tail.

3, if the end is not specified, then the slice () method will select all elements from start to end of the array.


 

Example:

Use slice () method to select an existing element array

var ARR = [ "snow", "snow", "frost", "beginning of winter" ] 
the console.log (arr.slice ( l, 3 )); 
the console.log (arr.slice ( . 1 )); 
the console.log ( arr.slice ( -3, -1));

 

Two, splice () method

splice () method can be used to insert, delete or replace elements of the array .

grammar:

arrayObject.splice(index,howmany,element1,.....,elementX)

parameter:

    index , the necessary provisions to add or remove elements from where. , The index parameter is initially inserted and (or) Remove array elements, must be numeric.

    howmany , it is necessary. Specifies how many elements should be removed. Must be a number 0 is possible, if not specify this parameter, then remove all elements from index to the end of the original array.

    element1 , optionally, provisions a new element added to the array, the index mark at the beginning of the insertion indicated.

    elementX , alternatively, several elements may be added to the array.

return value:

If you remove an element from arrayObject, then it returns an array containing the elements to be deleted.

note:

       1, splice () method to remove zero or more elements from the beginning of the index, and with a plurality of values ​​or parameters declaration statement list to replace or modify those elements are removed.

       2、splice() 方法和 slice() 方法的作用是不同的,splice() 方法会直接对数组进行修改。


 

实例:

 删除从 index 2  开始的三个元素,并添加一个新元素来替代被删除的元素

var arr=["大雪","小雪","霜降","立冬","寒露"]
arr.splice(2,3,"秋分");
console.log(arr);

 创建一个新的数组,并且向里面添加元素

var arr = new Array(5)
arr[0] = "大雪"
arr[1] = "小雪"
arr[2] = "霜降"
arr[3] = "立冬"
arr[4] = "寒露"
arr.splice(4,0,"白露");
console.log(arr);

 

 

Guess you like

Origin www.cnblogs.com/nyw1983/p/11955078.html