DEV Community

Shivang Chauhan
Shivang Chauhan

Posted on

How to remove an element from an array?

Introduction

In this post I am going to discuss multiple ways which can be used to remove an element from an array, there are couple of ways which can be used to achieve this. Some are splice, slice, filter etc, depending on the requirement we use different method to remove the element from an array.

Let's get started.

Using Array splice()

splice() can be used to do many things with an array like adding elements, removing elements, replacing the current element on any position, but in our case we are going to see how to remove an element from an array with splice().

const numbers = [1, 2, 3, 4, 5];
numbers.splice(1,1);
console.log(numbers) // [1, 3, 4, 5];

const deletedElement = numbers.splice(0,1);
console.log(deletedElement ) // [1];
Enter fullscreen mode Exit fullscreen mode

Explanation

Let's try to see what is going on in this example step by step, so basically splice() accepts two main arguments to remove an element from an array, first is the starting position which means from where in the array it needs to start modifying the elements (in our case removing them).

Then there is a second optional argument which is basically describing the number of elements to be removed from the starting position.

Using Array Filter()

This is another way to remove an element from an array, but in my opinion this is not necessary if we have a very simple array, like array of integers, letters, etc, this filter method is generally used to do complex filters on an array, anyways let's see how it works with an example.

const numbers = [1, 2, 3, 4, 5];
const modifiedArr = numbers.filter((el) => el !== 2);
console.log(modifiedArr); // [1, 3, 4, 5]
Enter fullscreen mode Exit fullscreen mode

Explanation

Let's try to understand this, filter() takes a callback function as an argument, this callback functions gets executed for each element in the array on which filter was called, this callback functions executes a condition for each element in an array which returns either true or false, if it returns true that elements gets pushed to the new array and if it returns false for any element then that elements gets skipped and not gets pushed to the new array.

Using Array Slice()

This is the final method which we are going to discuss, this method is mostly used when we want to extract a part of an array and leave previous and next elements, this is not our typical method to remove any element from any position in an array, so let's see how this method works and understand it's important arguments.

const numbers = [1, 2, 3, 4, 5];
const modifiedArr = numbers.slice(1,4);
console.log(modifiedArr); // [2, 3, 4]
Enter fullscreen mode Exit fullscreen mode

Explanation

As you can see in above example this method is used for very specific use case, so there are two main arguments which this method accepts, first is the starting position which is the index number from where we need to start taking the elements into our new array, in our example it is '1' which means second element, elements will be picked up to the second argument but slice() will not include that element at the index passed as the second argument.

Conclusion

In this post we saw different ways which can be used to remove an element from an array, each of these methods are used according to different use cases.

Check out more:

CRUD with DynamoDB, Nodejs and Serverless

DynamoDB VS MongoDB

What is AWS Artifact?

Javascript Math floor(), Math ceil() and Math round()

Top comments (1)

Collapse
 
ramjak profile image
Rama Jakaria • Edited

and my favorite's:

arr = [1,2,3,4]
arr.length = arr.length - 1
Enter fullscreen mode Exit fullscreen mode