Get the App
SLTechnology News&Howtos  ›  Development  › 

How JavaScript converts NodeList to an array

Shulou Source: shulou.com Published: 2022-06-01 05:13:17 10月27日 Update

Editor to share with you how JavaScript converts NodeList into arrays, I believe most people do not know much about it, so share this article for your reference, I hope you can learn a lot after reading this article, let's go to know it!

Convert NodeList to an array

If you run the document.querySelectorAll ("p") function, it may return an array of DOM elements, which is the NodeList object. But this object does not have the function of an array, such as sort (), reduce (), map (), filter (), and so on. In order for these native array functions to be used on it, you need to convert the list of nodes into an array. You can use [] .slice.call (elements) to do this:

Var elements = document.querySelectorAll ("p"); / / NodeList var arrayElements = [] .slice.call (elements); / / Now the NodeList is an array var arrayElements = Array.from (elements); / / This is another way of converting NodeList to Array is all about the article "how JavaScript converts NodeList into an array". Thank you for reading! I believe we all have a certain understanding, hope to share the content to help you, if you want to learn more knowledge, welcome to follow the industry information channel!

Tags: Arrays functions articles content functions objects not much that is elements most more knowledge nodes industry information information channels channels references learning help Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno MariaDB Microsoft macOS Linux Docker