ArticleDirectory
Today, I reviewed JavaScript and saw the array method. Two of them are similar: splice and splice. They look very similar, but they have a P, but their usage is quite different.
1. Slice
Slice is used to create a new array by specifying the elements in an array, that is, the original array will not change
VaRColor =NewArray ('red', 'Blue ', 'yellow', 'black');VaRColor2 = color. Slice (1, 2); Alert (color );//OutputRed, blue, yellow, black
Alert (color2 );//OutputBlue; Note: there is only one value for the second item.
2. splice
Splice is the most powerful method of array functions in JS. It can delete, insert, and replace array elements,The returned value is the operated value..
Splice deletion:Color. splice (1, 2)(Delete items 1 and 2 in color );
Splice insert:Color. splice (1, 0, 'Brown ', 'pink ')(Insert two values before the element whose color key value is 1 );
Splice replacement:Color. splice (1, 2, 'Brown ', 'pink ')(Replace element 1 and element 2 in color );
var color = New array ('red', 'blue', 'yellow ', 'black' ); var color2 = color. splice (000000, 'Brown ', 'pink' ); alert (color ); // Red, blue, brown, pink alert (color2 ); // yellow, black