Hey! I'm YCMJason, a Software Engineer in London 👨💻. Love diving into tech puzzles and sharing them! 🧩
All views expressed here are my own opinions, so please take them with a pinch of salt! 🧂
Eventually, I realized that timestamps was not an array, it was a NodeList and at the top of mdn documentation, ...
I seldom use selectElementsByClassName, but according to mdn it returns a HTMLCollection. And the mdn doc clearly says it hasn't got forEach.
But for NodeList, you should be able to do forEach. If you do document.querySelectorAll('.className'), you will get an NodeList and you should be able to do forEach. See here.
Howeverrrrr, since most older browsers won't have NodeList.prototype.forEach defined, it is probably safer to do what you suggested Array.prototype.forEach.call(elements, ...) or just [].forEach.call(elements, ...). A more "es6" way would probably be Array.from(elements).forEach(...).
Orrrrr, you could do it with for-loops as you suggested. "es6" introduced this amazing for-of loop, it could loop through most list-like things. So the following would work as well.
for(constelofelements){// ...}
Of course, to safely use ES6 features you probably want your polyfills + babel set up to support old browsers.
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.
Wait, is this true?
I seldom use
selectElementsByClassName
, but according to mdn it returns aHTMLCollection
. And the mdn doc clearly says it hasn't gotforEach
.But for
NodeList
, you should be able to doforEach
. If you dodocument.querySelectorAll('.className')
, you will get anNodeList
and you should be able to doforEach
. See here.Howeverrrrr, since most older browsers won't have
NodeList.prototype.forEach
defined, it is probably safer to do what you suggestedArray.prototype.forEach.call(elements, ...)
or just[].forEach.call(elements, ...)
. A more "es6" way would probably beArray.from(elements).forEach(...)
.Orrrrr, you could do it with for-loops as you suggested. "es6" introduced this amazing
for-of
loop, it could loop through most list-like things. So the following would work as well.Of course, to safely use ES6 features you probably want your polyfills + babel set up to support old browsers.