1

Please forgive me in advance as I'm sure this is a noob question (I honestly searched google for an hour before I cam here);

Long Story:

Okay, so I'm creating an application that lists vehicles stored in a database. I have a JavaScript class called Car that is instantiated for each car listing. The class contains default values for everything (make, model, year, color, price, etc) and has a function to use a jQuery ajax request to have my php return the actual values for the car. I pass it with JSON and have a consistent naming convention, so my xhr object in my success function is basically a copy of the car object, only with the correct values. I'm trying to find a simple way to give the xhr object's properties to the car object without manually specifying all 30 options (I know it's doable, but I'm sure there must be a better method, and i'm always up to learn)

Short Story:

How can I use object-x's properties to overwrite object-y's when they have the exact same list of defined properties and both already exist?

Thank you very much in advance!

Elie Zeitouni
  • 251
  • 1
  • 4
  • 13

1 Answers1

3

Well you would simply

$.extend( car, json );

Where car object is the target and json is the object where the correct values are

Docs http://api.jquery.com/jQuery.extend/

Jsfiddle demo http://jsfiddle.net/qApyZ/

Esailija
  • 138,174
  • 23
  • 272
  • 326