How do I convert Array to Object in JavaScript?

Problem Statement

I have an array.

['a', 'b', 'c']

How do I convert it into the following by JavaScript?

{
    0: 'a',
    1: 'b',
    2: 'c'
}

Solution

This should do it.

Object.assign({}, ['a', 'b', 'c']);

or

{...['a', 'b', 'c']}

Leave a Comment

Your email address will not be published. Required fields are marked *