Questions And Answers

More Tutorials

Javascript Arrays

Converting Array-like Objects to Arrays

JavaScript has "Array-like Objects", which are Object representations of Arrays with a length property. For example:

var realArray = ['a', 'b', 'c'];
var arrayLike = {
 0: 'a',
 1: 'b',
 2: 'c',
 length: 3

Common examples of Array-like Objects are the arguments object in functions and HTMLCollection or NodeList objects returned from methods like document.getElementsByTagName or document.querySelectorAll.

Convert Array-like Objects to Arrays in ES6

1. Array.from:

const arrayLike = {
 0: 'Value 0',
 1: 'Value 1',
 length: 2
const realArray = Array.from(arrayLike);
realArray.forEach(value => {/* Do something */}); // Works

2. for...of:

var realArray = [];
for(const element of arrayLike) {

3. Spread operator:


4. Object.values:

var realArray = Object.values(arrayLike);

5. Object.keys:

var realArray = Object
 .map((key) => arrayLike[key]);

Modifying Items During Conversion

In ES6, while using Array.from, we can specify a map function that returns a mapped value for the new array being created.

Array.from(domList, element => element.tagName); // Creates an array of tagName's


In this page (written and validated by ) you learned about Javascript Arrays . What's Next? If you are interested in completing Javascript tutorial, your next topic will be learning about: Javascript Reducing values.

Incorrect info or code snippet? We take very seriously the accuracy of the information provided on our website. We also make sure to test all snippets and examples provided for each section. If you find any incorrect information, please send us an email about the issue:

Share On:

Mockstacks was launched to help beginners learn programming languages; the site is optimized with no Ads as, Ads might slow down the performance. We also don't track any personal information; we also don't collect any kind of data unless the user provided us a corrected information. Almost all examples have been tested. Tutorials, references, and examples are constantly reviewed to avoid errors, but we cannot warrant full correctness of all content. By using, you agree to have read and accepted our terms of use, cookies and privacy policy.