最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - array.slice(-1)[0] -- Can someone explain? - Stack Overflow

programmeradmin3浏览0评论

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?

Share Improve this question edited Jul 20, 2017 at 22:20 cнŝdk 32.1k7 gold badges60 silver badges80 bronze badges asked Jul 20, 2017 at 22:07 Austin GrinerAustin Griner 1991 gold badge2 silver badges6 bronze badges 2
  • 3 developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/… – kind user Commented Jul 20, 2017 at 22:08
  • 1 Open up your console and take a look at array.slice(-1), array.slice(-2), etc. – Ry- Commented Jul 20, 2017 at 22:11
Add a comment  | 

3 Answers 3

Reset to default 16

Break 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).

发布评论

评论列表(0)

  1. 暂无评论