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
Rocket Launch Your Career
Speed up your learning progress with our mentorship program. Join as a mentee to unlock the full potential of Webtips and get a personalized learning experience by experts to master the following frontend technologies: