
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Remove Particular Element from Array in JavaScript
In JavaScript, Removing a particular element from an array refers to the process of deleting or filtering out a particular element from the array. so that a element is no longer exists within that array.
The Arrays are the most commonly used data structures in JavaScript, allowing developers to store and manipulate collections of items.
Syntax
Following is the syntax to remove a particular element from an array.
array(index, deleteCount[, element1[, element2[, ...]]])
Parameters
Here, we have three parameters to remove a particular element from an array in JavaScript ?
- index ? The index at which to start changing the array.
- deleteCount ? The number of elements to remove.
- element1, ..., elementN(optional) ? The elements to add to the array.
Return Value
It returns an array that removes a particular element from the original array.
Removing a particular element from an array in JavaScript
Removing a particular element from an array in JavaScript is quite easy. Let's learn through the following programs ?
Using Splice() Method
The splice() Method is used to remove one or more elements from an array.
Example
let array = [1, 2, 3, 4, 5]; let array = [1, 2, 3, 4, 5]; let index = 2; if (index > -1) { array.splice(index, 1); } console.log(array);
Output
The above program produce the following result ?
[ 1, 2, 4, 5 ]
Using filter() Method
This filter() method is used to filter out the elements based on a condition.
Example
let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; let filteredArray = array.filter(item => item !== valueToRemove); console.log(filteredArray);
Output
Following is the output of the above program ?
[ 1, 2, 4, 5 ]
Using slice() Method
Here, the slice() method is used to extract a portion of an array without modifying the original array.
Example
let array = [1, 2, 3, 4, 5]; let valueToRemove = 3; let index = array.indexOf(valueToRemove); let newArray = array.slice(0, index).concat(array.slice(index + 1)); console.log(newArray);
Output
Following is the output of the above program ?
[ 1, 2, 4, 5 ]