Remove First, last Element From Array Javascript

Remove First, last Element From Array Javascript

In this tutorial, you will learn how to remove the element or item from the javascript array.

JavaScript has various methods to remove element from JavaScript arrays. Here we will take e.g., remove a specific element from array javascript, remove the first element from array javascript, remove last element from array javascript and remove the object from json array javascript.

If we 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 for remove elements from JavaScript arrays.

Remove first, last or specific element from JavaScript arrays

There are four javascript built-in methods available to remove elements from an array, see the following:

  • pop() JavaScript Method – Remove last element from array javascript
  • shift() JavaScript Method – Remove first element from array javascript
  • splice() JavaScript Method – Remove specific element from array javascript
  • filter() JavaScript Method – Allows you to programmatically remove elements from an Array

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" ]; 

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.

We have an array object, we 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.

Recommended JavaScript Tutorials

Recommended:-JavaScript Arrays

AuthorAdmin

My name is Devendra Dode. I am a full-stack developer, entrepreneur, and owner of Tutsmake.com. I like writing tutorials and tips that can help other developers. I share tutorials of PHP, Python, Javascript, JQuery, Laravel, Livewire, Codeigniter, Vue JS, Angular JS, React Js, WordPress, and Bootstrap from a starting stage. As well as demo example.

Leave a Reply

Your email address will not be published. Required fields are marked *