How can I merge 2 javascript objects, populating the properties in one if they don't exist in the other?

后端 未结 5 1833
醉酒成梦
醉酒成梦 2021-02-18 17:08

If I have a javascript object/assoc. array defined like this:

function somefunction(options) {

    var defaults = {
        prop1: \'foo\',
        prop2: \'bar         


        
5条回答
  •  余生分开走
    2021-02-18 17:29

    After re-reading the question, I realized you're probably looking for something more like this:

    var a = { 'foo': 'bar', 'baz': 'bat' };
    var b = { 'foo': 'quux' };
    for (var prop in a) {
        if (prop in b) { continue; }
        b[prop] = a[prop];
    }
    

提交回复
热议问题