How to create properties from variables in JSON
Some times we need create a JSON and his properties from variables and not from fixed values, this properties can be used at future as some kind of index and will be a lot easier get information without loops need.
It’s really simple, we can create object, properties from variables and assign it a value:
To see all this object converted in string, just put it out in a console with
Now starts my favorite part … access information without loops. We are using ‘user’ property as index then we can go directly to get it.
That’s all folks!
Another useful sources:
Is it at all possible to use variable names in object literal properties for object creation? Example function…stackoverflow.com