How to Convert an Array-Like Object Into an Array in JavaScript?

Learn how to convert to an array from an array-like object in JavaScript

You can convert an array-like object into a JavaScript array in the following ways:

Using Array.from()

You can simply use Array.from() (introduced in ES6) to convert an array-like object into an array. For example:

// ES6+
const obj = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };

console.log(Array.from(obj)); // output: ['foo', 'bar', 'baz']

Using Array.prototype.slice()

If for some reason you cannot support ES6, you can instead use Array.prototype.slice() in the following way:

const obj = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
const arr =;

console.log(arr); // output: ['foo', 'bar', 'baz']

Using a for Loop

Since array-like objects are indexed elements having a length property, you can simply iterate over its elements and copy them into a new array like so:

const obj = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };
const arr = [];

for (let i = 0; i < obj.length; i++) {
    arr[i] = obj[i];

console.log(arr); // output: ['foo', 'bar', 'baz']

While a loop can also be used to iterate over the items in an array-like object, it should be avoided because it will also enumerate its other enumerable properties (such as the length property, etc.).

Using the Spread Operator

Converting an array-like object into an array using the spread operator (which was introduced in ES6) only works on objects that are iterable. To demonstrate this, let's consider the following example with an array-like object that does not implement the iterable protocol:

const obj = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };

// TypeError: obj is not iterable
console.log([ ...obj ]);

Now, let's look at the following example where we implement an iterator:

// ES6+
const obj = { 0: 'foo', 1: 'bar', 2: 'baz', length: 3 };

obj[Symbol.iterator] = function* () {
    for (let i = 0; i < this.length; ++i) {
        yield this[i];

console.log([ ...obj ]); // output: ['foo', 'bar', 'baz']

As you can see, by making the array-like object iterable, we can use the spread operator to convert it into an array. Almost all in-built array-like objects already have an iterator. Therefore, it is possible to use the spread operator to convert them into an array. However, for custom array-like objects, you would have to implement the iterator protocol as shown in the example above.

For unpacking large objects, the spread object might not be the most performant/optimal choice as it may lead to a stack overflow / range error.

Hope you found this post useful. It was published . Please show your love and support by sharing this post.