Reputation: 262
I need to create an object which has a constant first key property. and the rest are dynamic (comes from db) my solution is.. lets say like this:
var bar="bar"; //comes from db;
var myProperties="trick":"treat","man":"woman"; // comes from db
var obj = {"foo":bar, myProperties}
but i had an error. and i am not sure how to deal with this requirement.
anyone? alternative solution?
Upvotes: 0
Views: 74
Reputation: 377
var object = new Object(); var obj = {"foo":bar}
var myProperties="trick":"treat","man":"woman"; // comes from db
var object = myProperties.slipt(",");
for(var i=0; i < object.length ; i++){
var item = object[i];
obj.push(item);
}
// obj = {"foo":bar, "trick": treat, "man": woman}
return obj;
Upvotes: 0
Reputation: 569
You should merge the properties:
function merge_options(obj1,obj2){
var obj3 = {};
for (var attrname in obj1) { obj3[attrname] = obj1[attrname]; }
for (var attrname in obj2) { obj3[attrname] = obj2[attrname]; }
return obj3;
}
var bar="bar"; //comes from db;
var myProperties={trick:"treat",man:"woman"}; // comes from db
var obj = merge_options({"foo":bar}, myProperties);
Or simpler create the object and set the foo property:
var obj={trick:"treat",man:"woman"};
obj.foo=bar;
Upvotes: 1