to remove first and last element in array

JavascriptArrays

Javascript Problem Overview


How to remove first and last element in an array?

For example:

var fruits = ["Banana", "Orange", "Apple", "Mango"];

Expected output array:

["Orange", "Apple"]

Javascript Solutions


Solution 1 - Javascript

fruits.shift();  // Removes the first element from an array and returns only that element.
fruits.pop();    // Removes the last element from an array and returns only that element. 

See all methods for an Array.

Solution 2 - Javascript

Creates a 1 level deep copy.

fruits.slice(1, -1)

Let go of the original array.

Thanks to @Tim for pointing out the spelling errata.

Solution 3 - Javascript

var fruits = ["Banana", "Orange", "Apple", "Mango"];
var newFruits = fruits.slice(1, -1);
console.log(newFruits); //  ["Orange", "Apple"];

Here, -1 denotes the last element in an array and 1 denotes the second element.

Solution 4 - Javascript

I use splice method.

fruits.splice(0, 1); // Removes first array element

var lastElementIndex = fruits.length-1; // Gets last element index

fruits.splice(lastElementIndex, 1); // Removes last array element

To remove last element also you can do it this way:

fruits.splice(-1, 1);

See Remove last item from array to see more comments about it.

Solution 5 - Javascript

push() adds a new element to the end of an array.
pop() removes an element from the end of an array.

unshift() adds a new element to the beginning of an array.
shift() removes an element from the beginning of an array.

To remove first element from an array arr , use arr.shift()
To remove last element from an array arr , use arr.pop()

Solution 6 - Javascript

You can use Array.prototype.reduce().

Code:

const fruits = ['Banana', 'Orange', 'Apple', 'Mango'],
      result = fruits.reduce((a, c, i, array) => 0 === i || array.length - 1 === i ? a : [...a, c], []);

console.log(result);

Solution 7 - Javascript

This can be done with lodash _.tail and _.dropRight:

var fruits = ["Banana", "Orange", "Apple", "Mango"];
console.log(_.dropRight(_.tail(fruits)));

<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.11/lodash.min.js"></script>

Solution 8 - Javascript

To remove element from array is easy just do the following

let array_splited = [].split('/');
array_splited.pop()
array_splited.join('/')

Solution 9 - Javascript

For avoiding Mutation and changing the source array, a ReactJS developer cannot use pop, shift function, because both will change the source array and this kind of changes are root of very deep bugs.

The best way is using slice JavaScript built-in function. This function will create a slice of the source array and return for you a new array, also it accept negative index numbers, it means the first negative number is the latest item in array. so:

fruits.slice(a, b)
  • The first argument means start from index a and the index a - 1 isn't wanted.
  • The second argument means slice end in index b - 1 and the index b isn't wanted.

So by the above explanation if we put a = 1 it means we don't want first element and if we put b = latest it means slicing will end in index b - 1 and we don't what index b.

: What was the index of latest element in the slice function? correct -1.

✅ So the final answer is:

fruits.slice(1, -1)

Solution 10 - Javascript

var resident_array =  ["RC_FRONT", "RC_BACK", "RC_BACK"];
var remove_item = "RC_FRONT";
resident_array = $.grep(resident_array, function(value) {
            return value != remove_item;
    });
resident_array = ["RC_BACK", "RC_BACK"];

Solution 11 - Javascript

Say you have array named list. The Splice() function can be used for both adding and removing item in that array in specific index i.e that can be in the beginning or in the end or at any index. On the contrary there are another function name shift() and pop() which is capable of removing only the first and last item in the array.

> This is the Shift Function which is only capable of removing the first element of the array

var item = [ 1,1,2,3,5,8,13,21,34 ]; // say you have this number series 
item.shift(); // [ 1,2,3,5,8,13,21,34 ];

> The Pop Function removes item from an array at its last index

item.pop(); // [ 1,2,3,5,8,13,21 ];

> Now comes the splice function by which you can remove item at any index

item.slice(0,1); // [ 2,3,5,8,13,21 ] removes the first object
item.slice(item.length-1,1); // [ 2,3,5,8,13 ] removes the last object 

> The slice function accepts two parameters (Index to start with, number of steps to go);

Solution 12 - Javascript

You used Fruits.shift() method to first element remove . Fruits.pop() method used for last element remove one by one if you used button click. Fruits.slice( start position, delete element)You also used slice method for remove element in middle start.

Solution 13 - Javascript

To remove the first and last element of an array is by using the built-in method of an array i.e shift() and pop() the fruits.shift() get the first element of the array as "Banana" while fruits.pop() get the last element of the array as "Mango". so the remaining element of the array will be ["Orange", "Apple"]

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionMohan RamView Question on Stackoverflow
Solution 1 - Javascriptuser180100View Answer on Stackoverflow
Solution 2 - JavascriptAnuragView Answer on Stackoverflow
Solution 3 - JavascriptTripti PantView Answer on Stackoverflow
Solution 4 - JavascriptChinokaoView Answer on Stackoverflow
Solution 5 - JavascriptHarun Or RashidView Answer on Stackoverflow
Solution 6 - JavascriptYosvel Quintero ArguellesView Answer on Stackoverflow
Solution 7 - JavascriptPenny LiuView Answer on Stackoverflow
Solution 8 - JavascriptRichieView Answer on Stackoverflow
Solution 9 - JavascriptAmerllicAView Answer on Stackoverflow
Solution 10 - Javascriptvikas palView Answer on Stackoverflow
Solution 11 - JavascriptLabib HussainView Answer on Stackoverflow
Solution 12 - Javascriptmanoj chauhanView Answer on Stackoverflow
Solution 13 - Javascriptchisom okoyeView Answer on Stackoverflow