To remove the first, last & specific element from the array javascript; in this tutorial, you will learn how to remove the first, last, and specific element or item from the javascript array.
JavaScript has various methods to remove elements from JavaScript arrays. This tutorial will show you various examples for removing a specific element from an array javascript using slice(), removing the first element from an array javascript using shift(), removing the last element from an array javascript using pop(), and removing the object from json array javascript using filter().
If you want to remove elements from the end of an array, from the beginning, from the middle, or from the particular index. So you can use the below methods to remove elements from JavaScript arrays.
Remove First, last and Specific Element From Array Javascript
You can easily remove the first, last, and specific element in an array in Javascript, for this, you can use “shift()” and “pop()” and “slice()” and “filter()” methods.
- pop() JavaScript Method – Remove the last element from the array of javascript
- shift() JavaScript Method – Remove first element from the array of javascript
- splice() JavaScript Method – Remove specific element from array javascript
- filter() JavaScript Method – Allows you to programmatically remove elements from an Array
- FAQs – How do I remove an object from JSON array javascript?
1. pop() JavaScript: Remove last element from an Array javascript
The javaScript pop method removes the last element of an array. javaScript pop() array method changes the length of the array.
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; arr.pop(); console.log( arr );
Result of the above example
["one", "two", "three", "four", "five", "six", "seven", "eight", "nine"]
2. shift() JavaScript: Remove first element from an Array javaScript
The JavaScript shift () method, which is used to remove the first element of the array javascript.
The shift method works the same as the pop method, except that it removes the first element of the JavaScript array instead of the last.
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; arr.shift(); console.log( arr );
The shift method removed only the first array element. When the element is removed the remaining elements are shifted down.
Result of the above example
["two", "three", "four", "five", "six", "seven", "eight", "nine", "ten"]
3. splice() JavaScript: Remove specific element from array javascript
The javascript splice() method is used to add or remove elements or items from an array javascript.
About the slice() method:
- The first argument specifies the location at which to begin adding or removing elements.
- The second argument specifies the number of elements to remove.
- The third and subsequent arguments are optional; they specify elements to be added to the array.
Let’s take an example of a splice method:
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; arr.splice(1,2); console.log( arr );
Result of the above example
["one", "four", "five", "six", "seven", "eight", "nine", "ten"]
4. filter() JavaScript: Allows you to programmatically remove elements from an Array
Using the Javascript array filter() method iterates through the array elements and removes elements from a given array.
See the following example:
Example:
var arr = [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; var filtered = arr.filter(function(value, index, arr){ return value > three; }); console.log( arr ); console.log( filtered );
Result of the above example
array ===> [ "one", "two", "three", "four", "five", "six", "seven", "eight", "nine", "ten" ]; Filtered ===> [ "four", "five", "six", "seven", "eight", "nine", "ten" ];
FAQs – How do I remove an object from JSON array javascript?
Using the javascript splice() method to remove Objects from Array in JavaScript. Let’s see the example below.
You have an array object and want to remove the id=3 object inside the array. So use the below method:
var arr = [ {id:1,name:'hello'}, {id:2,name:'world'}, {id:3,name:'cool'}, {id:4,name:'javascript'}, {id:5,name:'jquery'} ]; var ind = arr.findIndex(function(element){ return element.id===3; }) if(ind!==-1){ arr.splice(ind, 1) } console.log (arr);
Conclusion
In this tutorial, you have learned various javascript built-in methods to remove elements from arrays, remove a specific element from array javascript, remove first element from array javascript, remove last element from array javascript and remove object from json array javascript.