This content originally appeared on DEV Community and was authored by Shivang Chauhan
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];
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]
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]
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
Javascript Math floor(), Math ceil() and Math round()
This content originally appeared on DEV Community and was authored by Shivang Chauhan
Shivang Chauhan | Sciencx (2021-12-18T16:24:31+00:00) How to remove an element from an array?. Retrieved from https://www.scien.cx/2021/12/18/how-to-remove-an-element-from-an-array/
Please log in to upload a file.
There are no updates yet.
Click the Upload button above to add an update.