![]() |
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() methodThe splice method can be used to delete multiple elements by specifying the start index from where the deletion of the element will start and the number of elements to be deleted as parameters to it. This method will return the removed elements. Syntax:const remItems = array.splice(startIndex, numberOfElementsToBeDeleted); Example: The below code example will explain the use of the splice() method to delete elements from a TypeScript array.
Output: Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript Using the shift() methodThe 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.
Output: Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript Using the pop() methodThe pop() method will delete the last element of the array without passing any parameter. It returns the removed element as a result. Syntax:const remItem = array.pop(); Example: The below example will explain the use of the pop() method to delete element from an array.
Output: Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript Using filter() methodThe filter() method filter the array based on the condition of the passed callback function. It returns a new array with new length and new elements. Syntax:const newArr = array.filter(() => {}); Example: The below code implements the filter() method to remove element at 4th index from the TypeScript array.
Output: Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript Using the delete operatorThe delete operator can also be used to delete the array items in TypeScript. Syntax:delete array_name[deletingItemIndex]; Example: The below example explain the use of the delete operator to remove the item from an array in TypeScript.
Output: Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript Using the slice() methodThe slice() method can be utilized to remove an item from an array by creating a new array without the item to be removed. This method is useful when you want to keep the original array intact and obtain a modified array without modifying the original one. Syntax:const newArray = [...array.slice(0, indexToRemove), ...array.slice(indexToRemove + 1)]; Example: The following code demonstrates the use of the slice() method to remove an item from a TypeScript array.
Output: "Initial Array: JavaScript,1,GeeksforGeeks,2,TypeScript" Using the reduce() MethodAnother approach to remove an item from an array in TypeScript is by using the reduce() method. This method processes the array elements and builds a new array by accumulating values that meet a specific condition. Example: Below is an example demonstrating how to use the reduce() method to remove an item from an array based on a specific condition.
Output: Initial Array: [ 'JavaScript', 1, 'GeeksforGeeks', 2, 'TypeScript' ] |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 11 |