How do I transform an array into an object

0 votes
With the hep of code can you tell me How do I transform an array into an object?
Feb 10 in Node-js by Ashutosh
• 17,360 points
42 views

1 answer to this question.

0 votes

Here are some common approaches:

Using Array.prototype.reduce():

Example:

const array = ['a', 'b', 'c'];

const obj = array.reduce((accumulator, currentValue, index) => {

  accumulator[index] = currentValue;

  return accumulator;

}, {});

console.log(obj);

// Output: { '0': 'a', '1': 'b', '2': 'c' }

In this example, each element of the array becomes a value in the object, with its index as the corresponding key.

answered Feb 10 by Navya

Related Questions In Node-js

0 votes
1 answer

How do I perform an id array query in Mongoose?

Hello @kartik, Here is a mongoosey way to ...READ MORE

answered Nov 30, 2020 in Node-js by Niroj
• 82,840 points
6,344 views
0 votes
1 answer

How do I create a custom object in react?

Creating a custom popover in React enhances ...READ MORE

answered Dec 31, 2024 in Node-js by Navya
67 views
+2 votes
1 answer

How do I debug Node.js applications?

Hello @kartik, Use node-inspector  from any browser supporting WebSocket. Breakpoints, ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
1,137 views
0 votes
1 answer

How do I get the path to the current script with Node.js?

Hello @kartik, you can do this: fs.readFile(path.resolve(__dirname, 'settings.json'), 'UTF-8', ...READ MORE

answered Jul 8, 2020 in Node-js by Niroj
• 82,840 points
3,045 views
0 votes
1 answer

How do I set Resharper's language level for ECMAScript 6?

To configure ReSharper to recognize ECMAScript 6 ...READ MORE

answered Feb 10 in Node-js by Navya
43 views
0 votes
1 answer

What are the practical applications of ES6 WeakMap?

Practical Applications of WeakMap: Private Data Storage: Store private ...READ MORE

answered Feb 10 in Node-js by Navya
44 views
0 votes
1 answer

What is the correct method to clone a JavaScript object?

Cloning a JavaScript object can be achieved ...READ MORE

answered Feb 10 in Node-js by Navya
53 views
0 votes
1 answer

How can I check if an array contains a particular string in TypeScript?

You can use includes() method, which checks ...READ MORE

answered Feb 10 in Node-js by Navya
42 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP