proposal-array-last
proposal-array-last copied to clipboard
A JavaScript TC39 Proposal for getting the last element from an array
With ES2015 array destructuring, I find that `let [lastItem] = arr.slice(-1)` is a nice way to access the last item. It is hard to make a mistake with this syntax,...
I'm wondering if it would be better to follow the same pattern as `indexOf` and return `-1` for `lastIndex`, when there are no items in the array. Returning `0` would...
Although it seems that: ```javascript const last = array.lastItem; const last = array.item(-1); ``` both are equivalent, they are not: ```javascript array.lastItem = newValue; array.item(-1) = newValue; // it does...
I realize that this would be a trivial syntactic sugar for `array[0]`, but - Symmetry is good. It makes the language easier to learn and the code easier to write...
What about using `Array.prototype.lastElement()` as an alias of `Array.prototype.lastItem()`? The reason I suggest this is because usually when referring to the content of an Array, don't most coders say `element`...
I recently had the need to get the last-ish elements again. Twice actually: 1. Getting `matchIndex` from `String.p.replace` when using a function: ```js string.replace(pattern, (fullMatch, ...submatches, matchIndex, fullString) => {...
`.get(-1)` and `.set(-1, value)` have gotten a lot of support in #5, but since it's a separate proposal (and hasn't gotten a response from @keithamus yet), I figure it probably...
- more powerful (one syntax for many cases: `arr[^1]`, `arr[^2]`, etc.. or `arr[~1]` if this symbol is chosen) - more portable (it could apply to strings, we could use or...
Here's a simplified zero-dependency polyfill supporting all environments as far back as ES6, derived from [the official polyfill](https://github.com/keithamus/proposal-array-last#polyfill) at the time of writing. ```js const O = Object const floor...
The rationale would seem to apply equally to them.