1.slice () function
Definition and usage
The slice () method returns the selected element from an existing array.
Grammar
Arrayobject.slice (Start,end)
| Parameters |
Description |
| Start |
Necessary. Specify where to start the selection. If it is a negative number, it specifies the position from the end of the array. In other words, 1 refers to the last element, 2 refers to the second-lowest element, and so on. |
| End |
Optional. Specifies where to end the selection. The parameter is the array subscript at the end of the array fragment. If this parameter is not specified, then the segmented array contains all elements from start to end of the array. If this parameter is a negative number, it specifies the element starting from the end of the array. |
return value
Returns a new array containing elements from start to end ( excluding the element ) from the Arrayobject.
Description
Note that the method does not modify the array, but instead returns a subarray. If you want to delete an element from an array, you should use Method Array.splice ().
Example:
<script type= "Text/javascript" >varnew Array (6) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "arr[3] =" James "arr[4] =" Adrew "arr[5] =" Martin "+" <br/> ") document.write (Arr.slice (2,4) +" <br/> ") document.write (arr)</script >
Output:
George,john,thomas,james,adrew,martinthomas,jamesgeorge,john,thomas,james,adrew,martin
Arr itself does not change, but the return value after executing the function changes.
2.splice () function
Definition and usage
The splice () method adds/deletes an item to/from the array, and then returns the item that was deleted.
Note: This method changes the original array.
Grammar
Arrayobject.splice (index,howmany,item1,....., ItemX)
| Parameters |
Description |
| Index |
Necessary. An integer that specifies the location of the Add/remove item, using a negative number to specify the position from the end of the array. |
| Howmany |
Necessary. The number of items to delete. If set to 0, the item is not deleted. |
| Item1, ..., ItemX |
Optional. Adds a new item to the array. |
return value
| type |
Description |
| Array |
A new array containing the deleted items, if any. |
Description
The splice () method deletes 0 or more elements starting at index and replaces the deleted elements with one or more values declared in the argument list.
If the element is removed from the Arrayobject, the array containing the deleted element is returned.
Hints and Notes
Note : Notice that the splice () method works differently than the slice () method, and the splice () method modifies the array directly.
Example:
<script type= "Text/javascript" >varnew Array (6) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "arr[3] =" James "arr[4] =" Adrew "arr[5] =" Martin "+" <br/> ") deletearr=arr.splice (2,1," William ") document.write (arr+" <br/> ") document.write (Deletearr)</script>
Output:
George,john,thomas,james,adrew,martingeorge,john,william,james,adrew,martin
Thomas
Arr results in the deletion of the value after the specified element, while Deletearr is an array of deleted elements.
3.concat ()
Definition and usage
The Concat () method is used to concatenate two or more arrays.
The method does not alter the existing array, but only returns a copy of the concatenated array.
Grammar
Arrayobject.concat (Arrayx,arrayx,......, Arrayx)
| Parameters |
Description |
| Arrayx |
Necessary. The parameter can be a specific value, or it can be an array object. can be any number. |
return value
Returns a new array. The array is generated by adding all the Arrayx parameters to the Arrayobject. If the argument for the concat () operation is an array, then the element in the array is added, not the array.
Example:
<script type= "Text/javascript" >varnew Array (3) arr[0] = "George"arr[1] = " John "arr[2] =" Thomas "varnew Array (3) arr2[0] =" James "arr2[1] =" Adrew " arr2[2] = "Martin"document.write (Arr.concat (arr2))</script>
Output:
George,john,thomas,james,adrew,martin
ARR itself has not changed.
Slice (), splice (), concat () difference