HTMLCollections are array-like. They are iterable and have a length property. The slice function can convert array-like objects into Arrays. this is bound to our array-like object. slice iterates over this using the length property since no other arguments were given. All the elements are returned in a new Array then we can call forEach method on our Array
Is a good solution too if not better. timestamps is bound to this then forEach method iterates over the array-like object by using the length property. My solution would have looped through the array-like object once then the newly created array versus this solution which is once.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
I just want to make a few points.
for
loop in years.querySelectorAll
.getElementsByClassName
returns anHTMLCollection
, which is why you were getting the error.NodeList
s have aforEach
method andHTMLCollection
s do not.My initial instinct on circumventing this is the following:
HTMLCollection
s are array-like. They areiterable
and have alength
property. Theslice
function can convert array-like objects intoArray
s.this
is bound to our array-like object.slice
iterates overthis
using thelength
property since no other arguments were given. All the elements are returned in a newArray
then we can callforEach
method on ourArray
But looking at your initial answer:
Is a good solution too if not better. timestamps is bound to
this
thenforEach
method iterates over the array-like object by using thelength
property. My solution would have looped through the array-like object once then the newly created array versus this solution which is once.