Skip to content

caub/proposal-array-nth-last

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 

Repository files navigation

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

Array.prototype[Symbol.index](n, fromEnd = false) { }

The existing someArray[n] desugars tosomeArray[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:

About

A JavaScript TC39 Proposal for getting the nth element from the end of an array: array[^n]

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published