Is it possible to add a property to a javascript / json object by using a value from a string?
let myObj= {};
for{prop in propsToAdd){
myObj.addProp(prop.name, prop.type);
}
myObj.addProp = function (name, type) {
// here i need to add another json object
// with the name as the name of the property
// and a property called type with the value of the param type
}
example:
myObj = {}
myObj.addProb("title","string");
myObj.addProp("id","integer")
should result as same as:
myObj = {
"title": {
"type": "string"
},
"id": {
"type": "integer"
},
}
I was thinking of the use of JSON.stringify
(building strings together) and JSON.parse
.
But it would be good if there's a more elegant way.