Categories
JavaScript Answers

How to convert array to object with JavaScript?

Spread the love

To convert array to object with JavaScript, we use the Object.entries and Object.fromEntries methods.

For instance, we write

const arr = ["a", "b", "c"];
const obj = Object.fromEntries(Object.entries(arr));

to call Object.entries with arr to convert arr to an array of key-value pair arrays with the index as the key and the element as the value.

Then we call Object.fromEntries to convert the array to an object with the key as the property key and the values as the property values.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published.