JavaScript to Remove Element in Array
JavaScript arrays are variables that can store ordered collection of values in them which can be of different datatypes, with each having a numeric position known as indexes. We can add and remove array elements in several ways.
So, how do we remove a particular element in JavaScript array?
There are different methods and how we can remove elements from a JavaScript array using built-in functions like:
- shift() – remove elements from the beginning an array
- pop() – remove elements at the end of an array
- delete – elements can be deleted using delete operator as JavaScript arrays are objects
- length – by setting the length of the array less than the original array
- splice() – remove the element from a specific array index
1.Using shift() method to remove elements from the beginning of the array
The shift method selects the first element and it is removed from the array, it takes no parameters and when the element is removed the remaining elements are shifted.
It returns the element that has been removed from the array, modifies the array on which it is invoked, also updates the length property.
Code:
var array = [1, 2, "cat", 4]; array.shift(); // returns 1 console.log( array ); // [2, "cat", 4]
2.Using pop() method to remove elements from the end of the array
The pop method pops out the last element of the array, updates the length property to one less than that of the original value, modifies the array on which it is invoked.
Code:
var array = [1, 2, "cat", 4, 5, "lion"]; array.pop(); // returns “lion” console.log( array ); // [1, 2, "cat", 4, 5]
3.Using delete operator to remove elements from the array
Using the delete operator we can remove specific array elements, it does not affect the length of the array, length remains unchanged and the elements deleted from the array is replaced by an undefined value. The reason for this behaviour is delete removes properties from the JavaScript objects and arrays are treated as objects in JavaScript.
Code:
var array = [1, 2, "cat", 4, 5, "lion"]; delete array[4]; // delete element with index 4 console.log( array ); // [1, 2, "cat", 4, undefined, "lion"]
4.Using length operator to remove elements from the array
By using the length property we can remove the elements from the end of the array. Setting the length property to a value less than the original array. Any element whose index is equal or greater than the value will be removed.
Code:
var arrray = [1, 2, "cat", 4, 5, "lion"]; array.length = 4; // set length to remove elements console.log( array ); // [1, 2, "cat", 4]
5.Using splice() method to remove elements from the array
Splice method is used to remove or add elements to an array.
It takes two arguments. First specifies the location from which to begin adding or removing elements. Second specifies the number of elements to remove. It takes the third argument which is an optional one which specifies the elements to be added to the array.
Code:
var array = [1, 2, "cat", 4, 5, 6, 7, 8, "lion", 0]; var removed = array.splice(2,3); console.log(removed); /* removed === ["cat", 4, 5] arr === [1, 2, "lion", 7, 8, "tiger", 0] */
You can use one of these methods that suits you to delete or remove particular element from a JavaScript array.