I'm thinking this must be a common problem but can't seem to find the solution. Using JSON config files to extend a jQuery object that contains objects and arrays.
For the objects and simple properties, I want to overwrite (as extend
does nicely).
For the arrays there may or may not be existing items.
Currently an array just overwrites the first elements
var sourceObj = {propterty:"change Me",anArray:[{name:"first"},{name:"second"}]},
configJSON = '{"propterty":"New Val","anArray":[{"name":"third"}]}',
configObj = JSON.parse(configJSON);
$.extend(true,sourceObj,configObj);
This returns:
{propterty:"New Val" , anArray:[{name:"third"},{name:"second"}}
Can I instead get:
{propterty:"New Val",anArray:[{name:"first"},{name:"second"},{name:"third"}]}
while ALSO allowing for updating "first" and "second" objects?
"anArray":[{"name":"second","newProp":"add newProp to second"}]
Could/should extend
be modified to compare array items and extend or add based on some rule or set property value such as "name"?
Thanks for any advice or pointers.