You can do this using .slice()
method like:
arr.slice(0, -1); // returns [1,0]
Here is a demo:
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_
instead of doing :
arr.slice(-1); // returns [2]
Here is a demo:
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_
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!