JavaScript Remove First, Last Element From Array

To remove first, last & specific element from array javascript; Through this tutorial, i am going to show you how to remove the first, last and specific element or item from the javascript array.

JavaScript Remove First, last and Specific Element From Array

There are four javascript built-in methods to remove first, last and specific elements from an array; as shown below:

  1. pop() JavaScript Method – Remove last element from array javascript
  2. shift() JavaScript Method – Remove first element from array javascript
  3. splice() JavaScript Method – Remove specific element from array javascript
  4. filter() JavaScript Method – Allows you to programmatically remove elements from an Array
  5. 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.

More JavaScript Tutorials

Recommended:-JavaScript Arrays

Leave a Comment