building an object in javascript using properties from another object -


what trying create object has x number of properties in along y number of properties object retrieve calling method. possible do?

ie here pseudo code. want mydata include properties object getmoredata() maybe not possible have no idea how if :(

var mydata =  {     name: "joe soap",     dob: "01-01-2001",     otherdata: {         hascat: true,         hasdog : false     }      var otherdata = getmoredata();     for(var prop in otherdata)     {         create additional property in mydata prop;     } }  function getmoredata() {     var moredata =      {         middlename: "tom",         location: "uk"     }     return otherdata; } 

you can't declare variables or use other statements for in middle of object literal. need define basic object first, , add properties afterwards:

var mydata = {     name: "joe soap",     dob: "01-01-2001",     otherdata: {         hascat: true,         hasdog : false     } };  var otherdata = getmoredata(); for(var prop in otherdata) {     mydata[prop] = otherdata[prop]; } 

also getmoredata() function needs return moredata variable, not otherdata:

function getmoredata() {     var moredata = {         middlename: "tom",         location: "uk"     }     return moredata; } 

demo: http://jsfiddle.net/nnnnnn/tqf5h/


Comments

Popular posts from this blog

monitor web browser programmatically in Android? -

Shrink a YouTube video to responsive width -

wpf - PdfWriter.GetInstance throws System.NullReferenceException -