How to use the shift() method In Typescript
The shift() method is used to remove the item from the start of an array and it returns the removed item as result. It requires no parameters.
Syntax:
const remItem = array.shift();
Example: The below example will explain the use of the shift() method to remove element from TypeScript array.
const testingArr: (number | string)[] =
["JavaScript", 1, "w3wiki", 2, "TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using the shift() method
const remItem = testingArr.shift();
console.log(`Removed Item: ${remItem}`)
console.log(`Array After removing item: ${testingArr}`);
Output:
Initial Array: JavaScript, 1, w3wiki, 2, TypeScript
Removed Item: JavaScript
Array After removing item: 1, w3wiki, 2, TypeScript
How do I Remove an Array Item in TypeScript?
In this article, we will learn about the different ways of removing an item from an array in TypeScript. In TypeScript, an array can be defined using union typing if it contains items of different types.
We can use the following methods to remove items from a TypeScript array:
Table of Content
- Using the splice() method
- Using the shift() method
- Using the pop() method
- Using filter() method
- Using the delete operator
- Using the slice() method