[javascript] Remove last item from array

You can do this using .slice() method like:

arr.slice(0, -1);    // returns [1,0]

Here is a demo:

_x000D_
_x000D_
var arr = [1, 0, 2];_x000D_
var newArr = arr.slice(0, -1);    // returns [1,0]_x000D_
_x000D_
console.log(newArr);_x000D_
$('#div1').text('[' + arr + ']');_x000D_
$('#div2').text('[' + newArr + ']');
_x000D_
<script src="http://code.jquery.com/jquery.min.js"></script>_x000D_
<b>Original Array    : </b>_x000D_
<div id="div1"></div>_x000D_
<br/>_x000D_
<b>After slice(0, -1): </b>_x000D_
<div id="div2"></div>
_x000D_
_x000D_
_x000D_

instead of doing :

arr.slice(-1);   // returns [2]

Here is a demo:

_x000D_
_x000D_
var arr = [1, 0, 2];_x000D_
var newArr = arr.slice(-1);    // returns [2]_x000D_
_x000D_
console.log(newArr);_x000D_
$('#div1').text('[' + arr + ']');_x000D_
$('#div2').text('[' + newArr + ']');
_x000D_
<script src="http://code.jquery.com/jquery.min.js"></script>_x000D_
<b>Original Array    : </b>_x000D_
<div id="div1"></div>_x000D_
<br/>_x000D_
<b>After slice(-1): </b>_x000D_
<div id="div2"></div>
_x000D_
_x000D_
_x000D_

Explanation:-

Now the basic syntax of Array.prototype.slice() or in short slice() method is:

arr.slice([begin[, end]])

Here,

the begin parameter is zero-based index at which extraction from an array starts. So, lets say based on above example if we do something like

arr.slice(0)    // returns [1,0,2]

it would return all the array elements from start of sequence from position 0 and that is [1,0,2]. Similarly, if we do

arr.slice(1)    // returns [0,2]

it would return [0,2] since 0 is at position 1 here and everything after that. Now, in your case you have passed a negative index i.e., -1 as the begin parameter, which indicates an offset from the end of the sequence. So, slice(-1) in your case extracts the last one array element in the sequence and that is 2 (as we have already seen in the above demo).

Now, let's talk about the end parameter in the slice() method syntax here. It is again a zero-based index at which extraction from an array ends. So, lets say we have a array like:-

var arr = [1, 0, 2, 5, 3, 9];

and we want to get just the 2,5,3 elements in the array. Now, position of 2 from start of the sequence is 2 and for last element 3 it is 4. We will need to end the extraction here a position 5, as we need to get the element before that position. So, we will simply implement slice() method here like

arr.slice(2, 5)    // returns [2,5,3]

In your case, we have implemented -1 as the end parameter, so our code is like

arr.slice(0, -1)   // returns [1,0]

As a negative index, end indicates an offset from the end of the sequence. So, slice(0,-1) extracts the first element through the second-to-last element in the sequence. So, we get the desired output. We can also do like

arr.slice(0, 2)    // returns [1,0]

we will get the same output. But, I have used -1 here as its easier to implement even for a long array like

[0,2,3,1,2,9,3,6,3,9,1,0,2,9,0,1,1,2,3,4,7,9,1]

If you just want to remove the last element, you don't want to sit & calculate the position of last 9 here and the do like arr.slice(0, 22). You can then simply implement the negative index logic here & do

arr.slice(0, -1) // same result as arr.slice(0, 22)

Hope it helps!

Examples related to javascript

need to add a class to an element How to make a variable accessible outside a function? Hide Signs that Meteor.js was Used How to create a showdown.js markdown extension Please help me convert this script to a simple image slider Highlight Anchor Links when user manually scrolls? Summing radio input values How to execute an action before close metro app WinJS javascript, for loop defines a dynamic variable name Getting all files in directory with ajax

Examples related to arrays

PHP array value passes to next row Use NSInteger as array index How do I show a message in the foreach loop? Objects are not valid as a React child. If you meant to render a collection of children, use an array instead Iterating over arrays in Python 3 Best way to "push" into C# array Sort Array of object by object field in Angular 6 Checking for duplicate strings in JavaScript array what does numpy ndarray shape do? How to round a numpy array?

Examples related to slice

How to search for an element in a golang slice Why can't I duplicate a slice with `copy()`? Correct way to initialize empty slice How to join a slice of strings into a single string? How to get the last element of a slice? Slice indices must be integers or None or have __index__ method Remove last item from array How do you clear a slice in Go? Concatenate two slices in Go how to get the last part of a string before a certain character?