Skip to content
Advertisement

How to create an object dynamically using JavaScript?

I’m trying to create a new object from an array of strings, but I’m not able to create the desired object correctly.

I’m trying to create an object like:

JavaScript

Here’s my code:

JavaScript

Advertisement

Answer

Yes.

First, we have to set result[item] to {} (a new, empty object):

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