Learning how to Remove element(s) from an array in TypeScript is an essential skill everyone should know when working with TypeScript arrays. We can use array.slice() method to remove n elements from an array. So how to do it? Let’s check it out!
Remove element(s) from an array in TypeScript
Remove the first/last n elements from an array
TypeScript has supported us with many built-in methods to work with arrays. One of them is the array.slice() method. The array.slice() method will extract a part in our array and return it to a new one.
Syntax:
array.slice(start, end)
Parameters:
- start (optional): the starting index/point you want to extract (default is 0).
- end (optional): the last index/point you want to extract.
Let’s try to apply it to solve our problems. Assume that we have an array of 8 elements and want to erase the first elements of the array. That means that we want our new array to receive all elements of the array but not the first one.
Completed code:
var myArr = [84, 57, 49, 53, 42, 56, 78, 90]; var result = []; result = myArr.slice(1); console.log(result);
Output:
[57, 49, 53, 42, 56, 78, 90]
If you want to remove the last elements:
var myArr = [84, 57, 49, 53, 42, 56, 78, 90]; var result = []; result = myArr.slice(0, -1); console.log(result);
Output:
[84, 57, 49, 53, 42, 56, 78]
Remove any elements from an array
Now, if we want to delete an element having its index, we will use the splice() method.
Syntax:
array.splice(index, howmany, element1,...)
Parameters:
- index: the index/position of the element we want to remove.
- howmany: the number of elements starting from that index.
- Element1,… (optional): the element that will be added to the array. If not, then splice() will serve as a delete function.
Completed code:
var myArr = [84, 57, 49, 53, 42, 56, 78, 90]; myArr.splice(4, 1); console.log(myArr);
Output:
[84, 57, 49, 53, 56, 78, 90]
Use array.filter()
If you want to remove an element that satisfies a condition from an array, then the array.filter() is a good choice. The array.filter() method will create a new array that only contains the elements of the old array that pass the test a function provides.
Syntax:
array.filter(function(currentValue, index, arr), thisValue)
Parameters:
- function(): a function or condition you want to run through every element.
- currentValue: the current element’s value.
- index: the current element’s index.
- arr: the current element’s array.
- thisValue: A value passed to the function as this value.
Example: We will create a program that removes all odd numbers from the array.
var myArr = [84, 57, 49, 53, 42, 56, 78, 90]; var result = []; function check(element: any) { return element % 2 === 0; } result = myArr.filter(check); console.log(result);
Output:
[84, 42, 56, 78, 90]
Summary
In this tutorial, we have described three different common methods to remove elements from an array in TypeScript. You can use the slice() method to delete the first/last n elements, the splice() method to delete any elements knowing their index, and the filter() method to eliminate the elements that satisfy a condition.
Maybe you are interested:
- Declare an Empty Array for a typed Variable in TypeScript
- Declare an Array with Fixed length in TypeScript
- How To Define An Array With Multiple Types In TypeScript

Hello. My name is Khanh Hai Ngo. I graduated in Information Technology at VinUni. My advanced programming languages include C, C++, Python, Java, JavaScript, TypeScript, and R, which I would like to share with you. You will benefit from my content.
Name of the university: VinUni
Major: EE
Programming Languages: C, C++, Python, Java, JavaScript, TypeScript, R