javascript - Copying AJAX JSON object into existing Object -


i have several javascript prototypes. initially, instances have id's filled in, generic place holder information other data. send message server id , object type (using jquery's ajax function) , server returns json object missing information (but no id). variables in returned object have exact same name in existing object.

what's easiest way transfer existing empty object? i've come few alternatives

  • set object equal returned object, copy in id (loses prototype functions?)
  • create function each object takes object identical structure , copies data
  • loop through key-value pairs of json object , copy them existing object

if use third option, correct way that? :

for (var key in json) {     if (object.hasownproperty(key)) {         object[key] = json[key];     } } 

assuming json returned object , object existing object.

try using extend():

var newobject = jquery.extend({}, oldobject); 

Comments

Popular posts from this blog

Perl - how to grep a block of text from a file -

delphi - How to remove all the grips on a coolbar if I have several coolbands? -

javascript - Animating array of divs; only the final element is modified -