Remove First and last Element From Array Javascript

Remove First and last Element From Array Javascript

In this tutorial, we would love to share with you, how to remove the element or item from the javascript array.

JavaScript has various methods to extract or 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.

To remove first, last or specific element from JavaScript arrays

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

pop() JavaScript: Remove last element from an Array javascript

The javaScript pop method remove the last element of the an array, returns that element. 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"]
 

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

splice() JavaScript: Remove specific element from array javascript

The javascript splice() method is used to add or remove element or item 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"]
 

filter() JavaScript: Allows you to programmatically remove elements from an Array

Filter () has a single parameter, a callback method. The callback is triggered because the filter method iterates through the array elements. It will pass three values ​​in the callback: the current value or element, the current array index and the full array.

The callback method must be either true or false. It is your responsibility to test the criteria (element) to see if it meets your criteria. If it does, you can do the truth. The elements that are returned are added to the new, filtered array.

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 methods to extract/remove elements from JavaScript arrays with example like 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.

You may like

  1. JavaScript Array splice() Method By Example
  2. JavaScript: Clone an Array & Object By Examples
  3. Sorting Using JavaScript Array Sort() Method
  4. JavaScript: Convert String to Array JavaScript
  5. javaScript Push() Array By Example
  6. javaScript Push Array, Items Into Array Example
  7. JavaScript Check the Object | isArray() Function
  8. JavaScript: Important Array Methods

Leave a Reply

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