$0.05

Convert Array to Object

Ask a question+
1

What is the best way to convert:

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

to:

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

2 Answers

0

ECMAScript 6 introduces the easily polyfillable Object.assign:

Object.assign({}, ['a','b','c']); // {0:"a", 1:"b", 2:"c"}

The own length property of the array is not copied because it isn’t enumerable.

Also, you can use ES6 spread syntax to achieve the same result:

{ ...['a', 'b', 'c'] }
add comment
0

Object.assign({}, [‘a’,’b’,’c’]); // {0:”a”, 1:”b”, 2:”c”}

add comment

Your Answer