Array.from()
The Array.from() and typed array .from() static methods copy items from an iterable or array-like object to make a new array.
Overview
The Array.from() and typed array .from() static methods copy items from an iterable or array-like object to make a new array.
Browser support
| Feature | Desktop | Mobile | ||||
|---|---|---|---|---|---|---|
| Chrome | Edge | Firefox | Safari | Chrome Android | Safari iOS | |
| 45 | 12 | 32 | 9 | 45 | 9 | |
| Built-in object | ||||||
| The TypedArray.from() static method creates a new typed array from an array-like or iterable object. This method is nearly the same as Array.from(). | 45 | 12 | 38 | 10 | 45 | 10 |
1+Supported (version) Not supported ※Has note Sub-feature descriptions sourced from MDN Web Docs (CC BY-SA 2.5)
Syntax
JAVASCRIPT
// NodeList → Array
const divs = Array.from(document.querySelectorAll('div'));
// String → Character array
Array.from('Hello'); // ['H', 'e', 'l', 'l', 'o']
// With mapping
Array.from({ length: 5 }, (_, i) => i * 2);
// [0, 2, 4, 6, 8] Live demo
mappingfunction use
generate and simultaneous to eachelement (example: index to generate)..
PreviewFullscreen
Use cases
Using Array.from()
The Array.from() and typed array .from() static methods copy items from an iterable or array-like object to make a new array.
Cautions
- No specific concerns. Stable across all major browsers.
Accessibility
- When updating the DOM dynamically, announce important changes to assistive technology with aria-live regions.
Related links
Powered by web-features