Skip to content
Advertisement

How to convert array to object and assign value in Javascript?

I have array value and object for data mapping. reduce method is not working for object mapping, here is sample data.

the array value is

JavaScript

object value is

JavaScript

need to form new object with the response

JavaScript

Advertisement

Answer

While I usually avoid recommending the usage of eval, this specific case seems to be a valid use-case for eval. You can simply extract the first item of the array as a variable name and extract the subsequent array items as levels:

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