How to Convert Array to Object in JavaScript
With the use of the spread operator, you can turn any array into an object where the key will be the index value:
Copied to clipboard! Playground
// Convert arrays to objects using the spread operator:
const JavaScriptSeries = ['π', 'π', 'π'];
const JavaScriptObject = { ...JavaScriptSeries };
console.log(JavaScriptObject);
// Results in the following:
{
0: 'π',
1: 'π',
2: 'π'
}
Note that object spread is not supported in IE.
Resources:
π More Webtips
Master the Art of Frontend
- Access 100+ interactive lessons
- Unlimited access to hundreds of tutorials
- Prepare for technical interviews