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 Sort an Array in TypeScript ?
Array sorting is the process of arranging the elements within an array in a specified order, often either ascending or descending based on predetermined criteria. Below are the approaches used to sort an array in typescript: Table of Content Method 1: Using sort methodMethod 2: Spread OperatorMethod
3 min read
How can I Define an Array of Objects in TypeScript?
In TypeScript, the way of defining the arrays of objects is different from JavaScript. Because we need to explicitly type the array at the time of declaration that it will be an Array of objects. In this article, we will discuss the different methods for declaring an array of objects in TypeScript.
6 min read
How to Declare an Array of Strings in TypeScript ?
Arrays are fundamental data structures in TypeScript, enabling developers to manage collections of elements efficiently. Below are the approaches to declare an Array of strings in TypeScript: Table of Content Square Brackets NotationArray ConstructorSquare Brackets NotationUsing square brackets nota
1 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. Examples Input: Arr = [ 10, 20, 30, 40, 50, 60 ] removeItem = 40 Output: [ 10, 20, 30, 50, 60 ]Table of Content Using splice() MethodUsing fi
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 lis
6 min read
How to Check if an Array Includes an Object in TypeScript ?
In TypeScript, checking if an array includes an object consists of comparing the object's properties within the array elements. We can compare and check this using three different approaches some method, find method, and includes method. There are several ways to check if an array includes an object
3 min read
How to Iterate Array of Objects in TypeScript ?
In TypeScript, we can iterate over the array of objects using various inbuilt loops and higher-order functions, We can use for...of Loop, forEach method, and map method. There are several approaches in TypeScript to iterate over the array of objects which are as follows: Table of Content Using for..
4 min read