Problem/Motivation

In the core, to get the last item in an array, use array[array.length - 1]. Modern JavaScript makes this code shorter.

Proposed resolution

Use array.at(-1).
For example, in /themes/claro/js/mobile.install.js

Before:

    if (steps[steps.length - 1].className === 'done') {

After:

    if (steps.at(-1).className === 'done') {

Remaining tasks

User interface changes

API changes

Data model changes

Release notes snippet

Issue fork drupal-3576258

Command icon Show commands

Start within a Git clone of the project using the version control instructions.

Or, if you do not have SSH keys set up on git.drupalcode.org:

Comments

tom konda created an issue. See original summary.

tom konda’s picture

Assigned: Unassigned » tom konda
tom konda’s picture

tom konda changed the visibility of the branch 3576258-prefer-to-replace to hidden.

tom konda’s picture

Assigned: tom konda » Unassigned
Status: Active » Needs work
tom konda’s picture

Status: Needs work » Needs review
smustgrave’s picture

Status: Needs review » Needs work
StatusFileSize
new71.27 KB

With the addition of default_admin believe there are other instances now.

more

tom konda’s picture

Status: Needs work » Needs review

Replaced array[array.length - 1] in default_admin theme.

smustgrave’s picture

Status: Needs review » Reviewed & tested by the community
Issue tags: +Needs Review Queue Initiative

Additional ones appear to be addressed.

quietone’s picture

Status: Reviewed & tested by the community » Needs work

What about this instance?

$ git grep  ".length - 1]"  | grep -v core/assets
core/modules/navigation/js/arrow-navigation.js:              elements[elements.length - 1].focus();

Is there a linting rule that can enforce this?