I have something similar to this:
...
meta: {
'orderby' : 'firstname',
'page' : 1,
'per' : 10
}
...
When I get send the request using ajax, part of my response contains some of this meta data. So I overwrite it with the new stuff. The server might send back something like:
meta: {
'page' : 1,
'per' : 10
}
The problem is that it overwrites the orderby key to be undefined. I don’t want to have the server send back everything, how can I leave a key’s value if the key isn’t sent back?
As you’ve said you’re using jQuery, you can use its
extendfunction:That will only overwrite properties in the target (
originalMeta, in the above) with properties from the source (returnedMetain the above) that actually exist. (No need to assign the result of the function tooriginalMeta, it’s modified in place.)It’s also dead easy without relying on jQuery:
That uses a
for..inloop to loop through all (enumerable) properites onreturnedMeta, filters out any it inherits from its prototype (it probably doesn’t inherit any, but…), and for ones that exist copies the values intooriginalMeta.