//Remove item by value
let array = [1, 2, 3, 4, 5];
let index = array.indexOf(3);
if (index > -1) {
array.splice(index, 1);
}
console.log(array); // [1, 2, 4, 5]
//Remove item by index
let array = [1, 2, 3, 4, 5];
array.splice(2, 1);
console.log(array); // [1, 2, 4, 5]
If you are working with an array in JavaScript. If you need to remove a specific element and item or element from an array by its value or index, then what will you do?
In this tutorial, you will learn how to remove specific elements or items from an array of javascript by value and index.
How to Remove a Specific Item/element from an Array in JavaScript by Value and Index
You can easily remove the specific element or item from the array by value and index in javascript using javascript array methods like filter(), indexOf(), and splice():
- Using the filter() Method
- Using the splice() Method
- Removing an Item from an Array by Index using the splice() method
Using the filter() Method:
You can remove specific elements or items or element from the array in JavaScript using the filter method.
Here is an example to remove an element from an array javascript by value using the filter() method; as follows:
let array = [1, 2, 3, 4, 5];
let valueToRemove = 3;
array = array.filter(item => item !== valueToRemove);
In the above example, the filter()
method creates a new array by excluding the elements with a value of 3. The resulting array will be [1, 2, 4, 5]
.
Using the splice() Method
Using the JavaScript indexOf() and splice() method, you can remove elements from an array of javascript by value.
Here is an example to remove an element from an array javascript by value using the js splice() and indexOf() method; as follows:
let array = [1, 2, 3, 4, 5];
let valueToRemove = 3;
let indexToRemove = array.indexOf(valueToRemove);
if (indexToRemove !== -1) {
array.splice(indexToRemove, 1);
}
In this example, indexOf()
is used to find the index of the item or element with the value 3. If the item exists in the array, the splice()
method is used to remove one element at the identified index.
Removing an Item from an Array by Index using the splice() method
To remove an item or element from an array based on its index, you can use the js splice()
method directly.
Here is an example to remove an element from an array javascript by index using the splice() method; as follows:
let array = [1, 2, 3, 4, 5];
let indexToRemove = 2;
array.splice(indexToRemove, 1);
The splice()
the method removes one element from the array at the specified index. In the example above, the item or element at index 2 (value 3) will be removed, resulting in the array [1, 2, 4, 5]
.
Here’s another example that demonstrates how to remove an item or elements by its index using the splice()
method:
const array = ['apple', 'banana', 'cherry', 'date']; const indexToRemove = 2; array.splice(indexToRemove, 1); console.log(array); // Output: ['apple', 'banana', 'date']
In this example, the splice()
method is used with the indexToRemove
and the number 1
to indicate that we want to remove only one element at the specified index.
Conclusion
Manipulating arrays is an everyday task in JavaScript development. When it comes to deleting specific items or elements by value or index, JavaScript provides several efficient ways to achieve the desired result. The filter() method is useful for removing items or elements by value, whereas the splice() method can be used to remove items or elements by both value and index. By understanding and using these techniques, you can confidently remove specific objects or elements from arrays in JavaScript while improving your programming skills and productivity.