How do I Remove an Array Item in TypeScript?
Last Updated :
01 Aug, 2024
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:
Using the splice() method
The 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.
JavaScript
const testingArr: (number | string)[] =
["JavaScript", 1, "GeeksforGeeks", 2, "TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using the splice() method
const remItem = testingArr.splice(3, 2);
console.log(`Removed Items: ${remItem}`)
console.log(`Array After removing item: ${testingArr}`);
Output:
Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript
Removed Items: 2, TypeScript
Array After removing item: JavaScript, 1, GeeksforGeeks
Using the shift() method
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.
JavaScript
const testingArr: (number | string)[] =
["JavaScript", 1, "GeeksforGeeks", 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, GeeksforGeeks, 2, TypeScript
Removed Item: JavaScript
Array After removing item: 1, GeeksforGeeks, 2, TypeScript
Using the pop() method
The 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.
JavaScript
const testingArr: (number | string)[] =
["JavaScript", 1, "GeeksforGeeks", 2, "TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using the pop() method
const remItem = testingArr.pop();
console.log(`Removed Item: ${remItem}`)
console.log(`Array After removing item: ${testingArr}`);
Output:
Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript
Removed Item: TypeScript
Array After removing item: JavaScript, 1, GeeksforGeeks, 2
Using filter() method
The 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.
JavaScript
const testingArr: (number | string)[] =
["JavaScript", 1, "GeeksforGeeks", 2, "TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using the fiter() method
const newArr = testingArr.filter((ele, ind) => ind !== 4);
console.log(`Removed Item: ${testingArr[4]}`);
console.log(`Array After removing item: ${newArr}`);
Output:
Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript
Removed Item: TypeScriptArray
After removing item: JavaScript, 1, GeeksforGeeks, 2
Using the delete operator
The 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.
JavaScript
const testingArr: (number | string)[] =
["JavaScript", 1, "GeeksforGeeks", 2, "TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using delete operator
console.log(`Removed Item: ${testingArr[0]}`);
delete testingArr[0];
console.log(`Array After removing item: ${testingArr}`);
Output:
Initial Array: JavaScript, 1, GeeksforGeeks, 2, TypeScript
Removed Item: JavaScript
Array After removing item: , 1, GeeksforGeeks, 2, TypeScript
Using the slice() method
The 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.
JavaScript
const testingArr: (number | string)[] = ["JavaScript", 1, "GeeksforGeeks", 2,
"TypeScript"];
console.log(`Initial Array: ${testingArr}`);
// Using the slice() method
const indexToRemove = 3;
const newArray = [...testingArr.slice(0, indexToRemove), ...testingArr.slice(
indexToRemove + 1)];
const removedItem = testingArr[indexToRemove];
console.log(`Removed Item: ${removedItem}`);
console.log(`Array After removing item: ${newArray}`);
Output:
"Initial Array: JavaScript,1,GeeksforGeeks,2,TypeScript"
"Removed Item: 2"
"Array After removing item: JavaScript,1,GeeksforGeeks,TypeScript"
Using the reduce() Method
Another 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.
JavaScript
const initialArray = ["JavaScript", 1, "GeeksforGeeks", 2, "TypeScript"];
const indexToRemove = 3;
const newArray = initialArray.reduce((acc, item, index) => {
if (index !== indexToRemove) {
acc.push(item);
}
return acc;
}, []);
console.log("Initial Array:", initialArray);
console.log("Array After removing item:", newArray);
Output:
Initial Array: [ 'JavaScript', 1, 'GeeksforGeeks', 2, 'TypeScript' ]
Array After removing item: [ 'JavaScript', 1, 'GeeksforGeeks', 'TypeScript' ]
Similar Reads
How to Clone an Array in TypeScript ? A cloned array is an array that contains all the items or elements contained by an already existing array in the same order. A cloned array does not affect the original array. If there is any change made in the cloned array it will not change that item in the original array. We can use the following
5 min read
How to Declare an Empty Array in TypeScript? TypeScript provides a robust type system that allows you to define and manage data structures more effectively than in plain JavaScript. One common task is declaring empty arrays with specific types, ensuring that your array holds only values of the intended type. These are the following ways to dec
2 min read
How to Convert a Set to an Array in TypeScript ? A Set in TypeScript is used to create a particular type of list that does not contain duplicate elements. If any element is repeated more than once it will automatically remove the duplicate existence and consider it only once in the list. In this article, we will convert these types of lists into a
5 min read
How to Remove a Specific Item from an Array in JavaScript ? Given an Array, the task is remove specific item from an array in JavaScript. It means we have an array with N items, and remove a particular item from array.ExamplesInput: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of ContentUsing splice() MethodUsing filte
3 min read
How to remove an item from an array in AngularJS Scope? In the AngularJS framework, we can manipulate the data that is within the scope, for example, we can perform the operations on an array by removing its elements. This helps us for better efficiency in developing the web application.We can remove the array elements in the applications like to-do list
5 min read
How to Remove Keys from a TypeScript Dictionary ? In TypeScript, we can remove keys from a TypeScript Dictionary using various approaches that include deleting keywords, Object Destructuring, and by using Object.keys() and Array.reduce() methods. There are several approaches to removing keys from a TypeScript Dictionary which are as follows: Table
3 min read
Check if an Array is Empty or not in TypeScript In TypeScript, while performing any operation on the array, it is quite important that there should be elements or data in the array, else no operation can be done. We can check whether the array contains the elements or not by using the below-listed methods: Table of Content Using length PropertyUs
2 min read
How to Remove a Key from TypeScript Object ? In TypeScript, objects are mutable, which means you can modify them by adding, updating, or removing properties. Removing a key from an object involves either creating a new object without the specified key or using certain built-in methods to achieve the desired result.Below are the approaches used
4 min read
How to Convert an Array of Objects into Object in TypeScript ? Converting an array of objects into a single object is a common task in JavaScript and TypeScript programming, especially when you want to restructure data for easier access. In this article, we will see how to convert an array of objects into objects in TypeScript.We are given an array of objects a
3 min read