admin管理员组文章数量:1208155
I got this code from codeblocq (never heard of the site):
alert(array.slice(-1)[0]);
It somehow returns the value of the last element in array
. The code works just as it should, but I have no idea how. Could anyone please help me understand this sorcery?
I got this code from codeblocq.com (never heard of the site):
alert(array.slice(-1)[0]);
It somehow returns the value of the last element in array
. The code works just as it should, but I have no idea how. Could anyone please help me understand this sorcery?
3 Answers
Reset to default 16Break the expression down into its parts. By understanding each small piece, you will understand the whole.
This is your original statement: alert(array.slice(-1)[0]);
alert(...)
is a function call. Before it can execute (and print something to the screen), its arguments must be evaluated first. It has one argument: array.slice(-1)[0]
, which we'll examine next.
array.slice(-1)
is another function call. [0]
is an array index. Which is evaluated first? To answer this, we turn to Operator Precedence. Both function calls and member access are level 19, with left-to-right associativity, which means we evaluate the function call first, then the array index next.
For this, let's turn to the documentation on array.slice
, which says:
arr.slice(begin)
A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence.
Return value
A new array containing the extracted elements.
So, array.slice(-1)
gives you an array containing the last element from the original array.
Moving left-to-right, we now have an array of a single item followed by an array index [0]
. That gives you the first (and only) item from the sliced array, which is then passed to the alert(...)
.
Check out Array.prototype.slice().
When you call array.slice()
, that's returning a slice of your array (as another array).
var array = ['zero', 'one', 'two', 'three'];
// Grab the elements from `array`
// beginning at 1 and up to (not including) 3.
var sliced = array.slice(1, 3);
console.log(array); // ['zero', 'one', 'two', 'three']
console.log(sliced); // ['one', 'two']
console.log(sliced[0]) // 'one'
In your code, you are executing array.slice(-1)
. The documentation says that "[a] negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence." Thus, your array.slice(-1)
is returning a new array populated with the last element of your original, array
.
var array = ['zero', 'one', 'two', 'three'];
var sliced = array.slice(-1);
console.log(array); // ['zero', 'one', 'two', 'three']
console.log(sliced); // ['three']
console.log(sliced[0]); // 'three'
// All together, it looks like this.
// I'm using `alert()` instead of `console.log()`
// to mirror your code.
alert(array.slice(-1)[0]); // 'three'
Actually the first argument for the Array.prototype.slice() method is the begin index of the shallow copy, if this index is negative it will extract the last values from this array based on the specified index (it indicates an offset from the end of the sequence).
So basically .slice(-n)
will return the last n
elements of the array.
If you take a look at the Parameters section of the .slice()
MDN Reference it says:
A negative index can be used, indicating an offset from the end of the sequence. slice(-2) extracts the last two elements in the sequence.
And this explains why you get the last value when you used -1
as an argument in array.slice(-1)
.
本文标签: javascriptarrayslice(1)0Can someone explainStack Overflow
版权声明:本文标题:javascript - array.slice(-1)[0] -- Can someone explain? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1738748845a2110276.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
array.slice(-1)
,array.slice(-2)
, etc. – Ry- ♦ Commented Jul 20, 2017 at 22:11