On This Page
Looping Utilities
The Looping utilities provide functions for iterating over arrays and objects in JavaScript. These functions offer flexible ways to loop through data structures, often providing more concise alternatives to traditional for loops.
Functions
each
Iterates over an object or array, invoking the provided function for each element.
Parameters
Name | Type | Description |
---|---|---|
obj | object/array | The object or array to iterate over |
func | function | The function to invoke for each element |
context | object | Optional. The context to bind the function to |
Returns
The original object or array.
Example
Flexible Iteration This function can iterate over arrays, array-like objects (e.g., NodeList), and plain objects. It uses
for...of
for arrays and array-likes, andObject.keys()
for plain objects, providing consistent behavior across different data types.
asyncEach
Asynchronously iterates over an object or array, invoking the provided function for each element.
Parameters
Name | Type | Description |
---|---|---|
obj | object/array | The object or array to iterate over |
func | async function | The async function to invoke for each element |
context | object | Optional. The context to bind the function to |
Returns
A Promise that resolves to the original object or array.
Example
asyncMap
Asynchronously maps over an object or array, invoking the provided function for each element and collecting the results.
Parameters
Name | Type | Description |
---|---|---|
obj | object/array | The object or array to map over |
func | async function | The async function to invoke for each element |
context | object | Optional. The context to bind the function to |
Returns
A Promise that resolves to a new object or array with the mapped values.
Example
These looping utilities provide powerful tools for iterating over and manipulating data structures in JavaScript, supporting both synchronous and asynchronous operations. They offer more flexibility and often more concise syntax compared to traditional looping methods.