Last Updated : 21 Nov, 2024
The following are different delete operations on a JS Array:
Delete from the Beginning of JS ArrayThis operation removes the first element of an array, shifting all other elements to lower indices. The array's length is reduced by one. Methods like shift() can be used for this, which directly mutates the array.
JavaScript
let a = [10, 20, 30, 40];
let res = a.shift();
console.log(res);
You can delete the first element of an array using many other methods. Refer to this article for more methods.
Delete from a Given Position in a JS ArrayTo remove an element from a specific index, you can use a method that targets that index. This operation deletes one or more elements starting at the specified index while shifting subsequent elements to fill the gap. The most commonly used method for this is splice().
JavaScript
let a = [10, 20, 30, 40];
let idx = 2;
let res = a.splice(idx, 1);
console.log(res);
You can delete an element from a specific index using many other methods. Refer to this article for more methods.
Delete an Element from the End of a JS ArrayThis operation removes the last element in the array, reducing its length by one. This is typically achieved using the pop() method, which mutates the original array and returns the removed element.
JavaScript
let a = [10, 20, 30, 40];
let res = a.pop();
console.log(res);
You can delete the last element of an array using many other methods. Refer to this article for more methods.
Delete First Occurrence from a JS ArrayTo delete the first occurrence of a specific value, the value is located within the array (e.g., using indexOf()), and then it is removed. This operation ensures that only the first matching element is deleted while leaving other instances of the value intact.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let idx = a.indexOf(x);
if (idx !== -1) {
a.splice(idx, 1);
}
console.log(a);
[ 10, 30, 20, 40 ]
You can delete the first occurrence of a specific value using many other methods. Refer to this article for more methods.
Delete last Occurrence from a JS ArrayThis operation targets the last occurrence of a specific value in the array. Using methods like lastIndexOf() to locate the value ensures that only the last matching element is removed, leaving earlier instances unaffected.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let idx = a.lastIndexOf(x);
if (idx !== -1) {
a.splice(idx, 1);
}
console.log(a);
[ 10, 20, 30, 40 ]
You can delete the last occurrence of a specific value using many other methods. Refer to this article for more methods.
Delete all Occurrences in a JS ArrayRemoves all instances of a specific value by filtering the array to exclude the unwanted value.
JavaScript
let a = [10, 20, 30, 20, 40];
let x = 20;
let res = a.filter(e => e !== x);
console.log(res);
You can delete all instances of a specific value using many other methods. Refer to this article for more methods.
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4