Skip to content
Advertisement

Javascript: How to convert array [“a=1”, “b=2”] into an object? [closed]

What is the best way to convert:

JavaScript

to

JavaScript

Advertisement

Answer

Here you want to convert an array to a single value( here it is an object ), for this more declarative way is to use reduce method. Here I have given an empty object as the initial value of the accumulator and on each iteration it adds the key value pair.

JavaScript

Hope this helps.

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement