![]() |
When working with HTML elements in JavaScript, you often get a NodeList, which is a collection of elements that match a certain selector. However, NodeLists don’t have built-in methods for filtering or mapping as arrays do. These are the following approaches: Using Array.from() and filter() MethodThis approach involves converting the NodeList to an array using Array.from() and then applying the filter() or map() method. Array.from() creates a new array instance from an array-like or iterable object, such as a NodeList. Example: This example shows the use of filter() method to filter the nodelist.
Output: ![]() Using Spread Operator and map() MethodThis approach involves using the spread syntax to convert the NodeList to an array and then applying the map() method. The spread syntax allows an iterable such as a NodeList to be expanded into individual elements. Example: This example shows the use of map() method to map the nodelist.
Output: The output will be shown in the console of Browser. ![]() |
Reffered: https://www.geeksforgeeks.org
JavaScript |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 18 |