0

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?

2 Answers 2

1

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;
Sign up to request clarification or add additional context in comments.

Comments

0

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;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.