githubEdit

Turn An HTMLCollection Into An Array

If you are using any of the built-in document query utilities, such as getElementsByClassName()arrow-up-right:

> document.getElementsByClassName("some-class")
HTMLCollection(5) [ ... ]

Then you are going to get an HTMLCollectionarrow-up-right in response. An instance of this cannot be iterated over, so you cannot call things like map and filter against it.

To use Array collection methods, you are first going to need to turn the HTMLCollection into an array. You can do this with Array.fromarrow-up-right:

> Array.from(document.getElementsByClassName("some-class"))
[ ... ]

sourcearrow-up-right

Last updated