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

后端 未结 5 1340
太阳男子
太阳男子 2021-02-18 16:47

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

    I'm a PrototypeJS holdout. Us Java-types prefer PrototypeJS's OO design over jQuery. Here's how you merge two Object/Hash/Maps with Proto:

    $H(obj1).merge(obj2).toObject()
    

    The input obj1 and obj2 are unaffected. obj2 map entries have precedence (i.e. obj1 and obj2 both have same key, the value for that key in obj2 will override).

提交回复
热议问题