Javascript Object Prototype Merge Object

Description

Javascript Object Prototype Merge Object


// Merges one object into another, preserving the original values if present
Object.prototype.merge = function(objSource) {
    // ensure that we are dealing with a valid object
    if (typeof this == "object" && objSource && typeof objSource == "object") 
        for (let arg in objSource)
            if (typeof objSource[arg] == "object" && !objSource[arg].length) {
                if (!this[arg])
                    this[arg] = {};/*from   w  w w .  j  av  a2 s.  c  o  m*/
                this[arg].merge(objSource[arg]);
            } else
                this[arg] = this[arg] || objSource[arg];
}

// Merge one object into another. We'll start with a generic definition of a person..
person = {
    name: "Unknown",
    age: 0,
    height: "Unknown",
    weight: "Unknown",
    occupation: "Unknown",
    children: {
        count: 0,
        names: []
    }
}

elvis = {
    name: "Elvis Presley",
    age: 57,
    occputation: "Rock Star"
}

// Now we merge person into elvis
elvis.merge(person);

//.. And test to see if one of the new properties were copied over
console.log("Elvis's Weight: " + elvis.weight); // "Unknown"



PreviousNext

Related