How to remove the first element from an array in TypeScript
How to remove the first element from an array in TypeScript:
An array is used to store values sequentially in TypeScript. Each element of an array can be accessed by its index. The index starts from 0 and ends at length of array - 1. So, the index of the first element is 0, 1 for the second element, 2 for the third element etc.
If you want to remove the first element from an array, we have different ways to do that in TypeScript. Let me show them one by one.
Method 1: By using shift():
shift method is used to delete the first element from an array. This is defined as like below:
shift()
It doesn't take any parameters. It deletes the first item from an array and returns that item.
So, we can call this method on an array and it will delete the first element from that array.
let givenArr = [1,2,3,4,5];console.log('Given Array: ',givenArr);givenArr.shift();console.log('Final Array: ',givenArr);
If you run this program, it will delete the first element of the array and it will print the below output:
Given Array: [1, 2, 3, 4, 5]Final Array: [2, 3, 4, 5]
By using slice():
slice is another method we can use to get one subarray from a given array. We can use this method to remove the first element from the array and create a new array holding the elements from index 1 to end.
This method is defined as like below:
slice(start?: number, end?: number)
It takes two parameters, the start and the end index. Both are optional values. If we don't provide start, it will pick from start or index = 0 and if we don't provide end, it will take it as the end index of the array.
It will return one copy of the subarray.
To remove the first element, we can provide start as 1 and we don't have to provide end because we want all elements to the end of the array.
let givenArr = [1,2,3,4,5];console.log('Given Array: ',givenArr);let newArray = givenArr.slice(1);console.log('Final Array: ',newArray);
slice will return one new array holding all elements from index = 1 to the end and we are assigning it to newArray.
If you run this, it will give similar output as the above example.
Given Array: [1, 2, 3, 4, 5]Final Array: [2, 3, 4, 5]
Using splice():
splice method is used to remove a given number of elements from an array. It takes the starting index and the number of elements to delete from the array as its parameters. It deletes all elements from the original array and returns one new array holding the deleted elements.
We can use splice to remove the first element from an array. splice is defined as like below:
splice(start: number, count?: number)
start is the index to start the deleting and count is the number of elements to delete from the array.
We need to pass start as 0 and count as 1 to delete the first element from the array.
let givenArr = [1,2,3,4,5];console.log('Given Array: ',givenArr);givenArr.splice(0,1);console.log('Final Array: ',givenArr);
It will print the same output:
Given Array: [1, 2, 3, 4, 5]Final Array: [2, 3, 4, 5]