S
S
Shimpanze2018-02-03 07:59:04
JavaScript
Shimpanze, 2018-02-03 07:59:04

JavaScript: Why doesn't getting the last element of an array work?

Hello!
Why doesn't getting the last element of an array work? Is this the shortest entry?

document.querySelectorAll('head > link[rel="stylesheet"]')[this.length-1];

Thank you!

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
Stalker_RED, 2018-02-03
@Shimpanze

What do you expect from this.length-1? Depending on the context of the call, you can have anything in this. If there is window, (window.length-1) == -1, and there cannot be an element with index -1 in the array.

let stylesheets = document.querySelectorAll('head > link[rel="stylesheet"]')
let last = Array.from(stylesheets).pop()
// или
let last = [...stylesheets].pop()

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question