Try with Array.reduce():

const arr = ['a','b','c'];
const res = arr.reduce((acc,curr)=> (acc[curr]='',acc),{});
console.log(res)
Run code snippetEdit code snippet Hide Results Copy to answer Expand

Answer from prasanth on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › javascript › convert-an-array-to-an-object-in-javascript
JavaScript - Convert an Array to an Object - GeeksforGeeks
July 11, 2025 - JavaScript · const a = ["Geeks", "for", "Geeks"]; const obj = {}; a.forEach((value, index) => obj[index] = value); console.log(obj); Output · { '0': 'Geeks', '1': 'for', '2': 'Geeks' } The reduce method is a powerful tool for transforming ...
Discussions

javascript - How to convert an array into an object? - Stack Overflow
If anyone else is looking for a ... lodash.com/docs#keyBy ... This is a bit confusing because arrays are objects already, but I guess the point of the question is converting the array exotic object to an ordinary object. ... A simple way to do this with Lodash is ... More on stackoverflow.com
🌐 stackoverflow.com
How to convert array to object with a constant key "id" .
You can use reduce or map, but map is easier in this case. let array = [100,200,300]; let newArray1 = array.map((v)=>{ return {id: v} }); console.log(newArray1); let newArray2 = array.reduce((acc, val)=>{ acc.push({id: val}); return acc },[]); console.log(newArray2); More on reddit.com
🌐 r/learnjavascript
29
16
October 6, 2022
Convert array to object in React JS
I’m working with arrays in a React application and need to convert an array into an object format. Can someone explain a method or provide a code sample on how to achieve this in JavaScript? Here’s a rough array structure: const items = [‘apple’, ‘orange’, ‘banana’]; I want to transform it into an object where each key ... More on community.latenode.com
🌐 community.latenode.com
0
0
October 13, 2024
Converting an array to a JSON object with custom key names
With and without Babel: https://jsfiddle.net/srxs4mcw/ The Babel summary: var arr = ["first", "second", "third", "fourth"]; var keys = ['one', 'two', 'three', 'four']; const newArr2 = arr.reduce((acc, val, i) => ({ ...acc, [keys[i]]: val }), {}); You reduce your array starting with an empty object. Each iteration spreads the accumulated object and adds the new array item to it. This one requires Babel to transpile your code. In my jsfiddle you'll also see a vanilla JS solution which is basically the same thing. More on reddit.com
🌐 r/webdev
6
3
April 21, 2017
🌐
C# Corner
c-sharpcorner.com › home › technologies › javascript › array to object in javascript: the best way to manipulate data
Array to Object in JavaScript: The Best Way to Manipulate Data
October 4, 2023 - Arrays are ordered collections of values, while objects are collections of key-value pairs. ... Let's dive in. To convert an array to an object, you can use the reduce() array method. This method iterates over the array and builds an object with keys and values based on your specified logic.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-array-to-object
Convert an Array to an Object using JavaScript | bobbyhadz
Which approach you should pick depends on whether you need to use the array's indexes as object keys or you need to use custom keys. Use the Object.assign() method to convert an array to an object.
🌐
CodeVertiser
codevertiser.com › convert-array-to-object-javascript
Convert Array to Object in JavaScript [5 Methods + Exercise]
April 12, 2025 - For making the same keys and values, ensure there isn't any repeated element in an array because a Javascript object cannot have two keys with the same name. This is one of the most practical examples where you convert array to object in JavaScript with keys based on a unique field like id.
🌐
Attacomsian
attacomsian.com › blog › javascript-convert-array-to-object
How to convert an array to an object in JavaScript
November 6, 2022 - Unlike the above methods, the Array.forEach() method allows you to name the keys of the new object. names.forEach((elem, i) => { obj[`key${i}`] = elem }) console.log(obj) // { key0: 'Alex', key1: 'Bob', key2: 'Johny', key3: 'Atta' } You can ...
Find elsewhere
🌐
MDN Web Docs
developer.mozilla.org › en-US › docs › Web › JavaScript › Reference › Global_Objects › Object › fromEntries
Object.fromEntries() - JavaScript - MDN Web Docs
October 30, 2025 - With Object.fromEntries, you can convert from Array to Object: ... const arr = [ ["0", "a"], ["1", "b"], ["2", "c"], ]; const obj = Object.fromEntries(arr); console.log(obj); // { 0: "a", 1: "b", 2: "c" } With Object.fromEntries, its reverse method Object.entries(), and array manipulation methods, you are able to transform objects like this: ... const object1 = { a: 1, b: 2, c: 3 }; const object2 = Object.fromEntries( Object.entries(object1).map(([key, val]) => [key, val * 2]), ); console.log(object2); // { a: 2, b: 4, c: 6 }
🌐
LabEx
labex.io › tutorials › array-to-object-based-on-key-28160
Convert Array to Object Based on Key | LabEx
Use Array.prototype.reduce() to create an object from the provided array. Use object destructuring to extract the value of the given key and the associated data, and then add the key-value pair to the object.
🌐
Medium
medium.com › dailyjs › rewriting-javascript-converting-an-array-of-objects-to-an-object-ec579cafbfc7
Rewriting JavaScript: Converting an Array of Objects to an Object. | by Chris Burgin | DailyJS | Medium
April 23, 2017 - The above code now accepts a keyField which allows us to use this function to convert any array of objects. Not just those who have an the field id.
🌐
DEV Community
dev.to › afewminutesofcode › how-to-convert-an-array-into-an-object-in-javascript-25a4
Javascript Array to Object: How to convert an array into an object in javascript - DEV Community
December 31, 2022 - Here's a slightly more concise version, where you throw out extra brackets and use an implicit return 😄 : const convertArrayToObject = (array, key) => array.reduce( (obj, item) => ({ ...obj, [item[key]]: item }), {} );
🌐
Medium
medium.com › @ryan_forrester_ › converting-arrays-to-objects-in-javascript-a-complete-guide-ddec52dcacb0
Converting Arrays to Objects in JavaScript: A Complete Guide | by ryan | Medium
November 8, 2024 - Each line explained: 1. We have two arrays: keys and values 2. The reduce method iterates over the keys array 3. For each key, we use its index to get the corresponding value 4. We build the object one property at a time 5. The final object matches each key with its value at the same index
🌐
Coding Beauty
codingbeautydev.com › home › posts › how to convert array values to object keys in javascript
How to Convert Array Values to Object Keys in JavaScript
May 23, 2023 - To convert array values to object keys in JavaScript, you can use the reduce() method. With reduce(), you can create a keyed object item for each array item. This results in an object with keys.
🌐
Bobby Hadz
bobbyhadz.com › blog › javascript-convert-array-values-to-object-keys
Convert an Array's Values to Object Keys in JavaScript | bobbyhadz
March 4, 2024 - Assign each array element as a key in the accumulator object. The reduce() method will construct an object from the array's values. ... Copied!const arr = ['name', 'age', 'country']; const obj = arr.reduce((accumulator, value) => { return ...
🌐
DEV Community
dev.to › calvin087 › convert-array-to-object-keys-58p3
Convert Array to Object Keys - DEV Community
December 19, 2021 - I wanted to take an array of elements and turn them into an object. The elements in the array would need to be the keys of the object with some default empty strings as the values to be changed later.
🌐
Stack Abuse
stackabuse.com › bytes › how-to-convert-an-array-to-an-object-with-javascript
How to Convert an Array to an Object with JavaScript
September 15, 2023 - Here's how you can use the spread operator to convert an array to an object: const array = ['apple', 'banana', 'cherry']; const object = { ...array }; console.log(object); ... No spam ever. Unsubscribe anytime. Read our Privacy Policy. ... As you can see, the array indices become the keys of ...
🌐
GeeksforGeeks
geeksforgeeks.org › convert-an-array-to-an-object-in-javascript
JavaScript – Convert an Array to an Object | GeeksforGeeks
November 19, 2024 - Here are the different methods ... to convert a two-dimensional array into an object is by iterating through the array using a for loop and assigning key-value pairs.JavaScriptconst a = [['name', 'Abhay...
🌐
Latenode
community.latenode.com › other questions › javascript
Convert array to object in React JS - JavaScript - Latenode Official Community
October 13, 2024 - Key-Value Assignment: For each item, assign the index as the key and the item as the value. Result: The resulting object will map each index to its corresponding item. This method efficiently converts your array into the desired object format ...
🌐
YouTube
youtube.com › watch
Convert an Array to Object in #javascript - YouTube
Learn how to transform an array into an object in JavaScript using the powerful reduce() method! 🚀 This quick tutorial will show you how to efficiently map ...
Published: October 19, 2024