https://github.com/caub/proposal-array-nth-last
A JavaScript TC39 Proposal for getting the nth element from the end of an array: array[^n]
https://github.com/caub/proposal-array-nth-last
Last synced: 9 months ago
JSON representation
A JavaScript TC39 Proposal for getting the nth element from the end of an array: array[^n]
- Host: GitHub
- URL: https://github.com/caub/proposal-array-nth-last
- Owner: caub
- Created: 2019-12-07T18:59:24.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2019-12-07T19:36:05.000Z (over 6 years ago)
- Last Synced: 2025-01-28T02:11:09.505Z (over 1 year ago)
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 4
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
Awesome Lists containing this project
README
# Getting nth-last item from an array
## Rationale
Currently the only ways to get an element from the end of an Array is with the `.length` property:`someArray[someArray.length - n]` or using the `.slice` method: `someArray.slice(-1)[0]`
## High Level Proposal
We propose to add the `Symbol.index` protocol to `Array`
```js
Array.prototype[Symbol.index](n, fromEnd = false) { }
```
The existing `someArray[n]` desugars to`someArray[Symbol.index](n)`
The new proposed syntax `someArray[^n]` desugars to `someArray[Symbol.index](n, true)`
This protocol could be added to `String` as well
## Specification
// TODO
## Related proposal
Compared to https://github.com/keithamus/proposal-array-last, the benefit in our proposal is to:
- avoid any web-compatibility issue
- avoid "magic" getters
- have a more powerful and user-friendly syntax, similarly to https://github.com/tc39/proposal-slice-notation