![]() |
VOOZH | about |
The Array.splice() is an inbuilt TypeScript function that changes the content of an array by adding new elements while removing old ones. It takes an index, a count of elements to remove, and optional elements to add, returning the removed elements and modifying the original array.
array.splice(index, howMany, [element1][, ..., elementN]); Parameter: This method accepts three parameters as mentioned above and described below:
Return Value: This method returns the extracted array.
Below example illustrate the Array splice() method in TypeScriptJS:
In this example The splice() method inserts 11 at index 2 in the array without removing any elements. removed is an empty array since no elements are deleted. The modified array is printed.
Output:
[]
In this example we are using splice() to remove the first 5 elements from the array arr, storing them in val, and then prints both val and the modified arr.
Output:
[ 'G', 'e', 'e', 'k', 's' ]
[
'f', 'o', 'r',
'g', 'e', 'e',
'k', 's'
]