最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

Javascript merge object with nested properties - Stack Overflow

programmeradmin0浏览0评论

Lets take a look at the example below:

var ref = {
    "fullName": {
        "rules": {
            "type": "string",
            "minLength": 4,
            "maxLength": 64
        },
        "description": "Full name of a user."
    }
};

var user = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
        },
        "message": "You have submitted a wrong full name."
    }
};

Now what I want is this:

  1. Merge objects & properties.
  2. Keep the properties of the second object IF they are set already (maxLength)

Below is the result that I expect:

var res = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
            "type": "string",
            "minLength": 4
        },
        "description": "Full name of a user.",
        "message": "You have submitted a wrong full name."
    }
};

What I have tried:

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    for (var propertyKey in firstObject) {
        var propertyValue = firstObject[propertyKey];
        
        if (typeof(propertyValue) === "object") {
            finalObject[propertyKey] = mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
        } else if (secondObject[propertyKey] === undefined) {
            finalObject[propertyKey] = firstObject[propertyKey];
        } else {
            finalObject[propertyKey] = secondObject[propertyKey];
        }
    }
    
    return finalObject;
}

The function above merges but somehow doesnt nest the properties.

UPDATE & ANSWER got it working, I forgot too itterate through the second object, how dumb. Thanks to @AnthonyGrist

function mergeProperties(propertyKey, firstObject, secondObject) {
    var propertyValue = firstObject[propertyKey];

    if (typeof(propertyValue) === "object") {
        return mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
    } else if (secondObject === undefined || secondObject[propertyKey] === undefined) {
        return firstObject[propertyKey];
    }
    
    return secondObject[propertyKey];
}

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    // Merge first object and its properties.
    for (var propertyKey in firstObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, firstObject, secondObject);
    }

    // Merge second object and its properties.
    for (var propertyKey in secondObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, secondObject, firstObject);
    }
    
    return finalObject;
} 

Lets take a look at the example below:

var ref = {
    "fullName": {
        "rules": {
            "type": "string",
            "minLength": 4,
            "maxLength": 64
        },
        "description": "Full name of a user."
    }
};

var user = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
        },
        "message": "You have submitted a wrong full name."
    }
};

Now what I want is this:

  1. Merge objects & properties.
  2. Keep the properties of the second object IF they are set already (maxLength)

Below is the result that I expect:

var res = {
    "fullName": {
        "rules": {
            "required": true,
            "maxLength": 128
            "type": "string",
            "minLength": 4
        },
        "description": "Full name of a user.",
        "message": "You have submitted a wrong full name."
    }
};

What I have tried:

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    for (var propertyKey in firstObject) {
        var propertyValue = firstObject[propertyKey];
        
        if (typeof(propertyValue) === "object") {
            finalObject[propertyKey] = mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
        } else if (secondObject[propertyKey] === undefined) {
            finalObject[propertyKey] = firstObject[propertyKey];
        } else {
            finalObject[propertyKey] = secondObject[propertyKey];
        }
    }
    
    return finalObject;
}

The function above merges but somehow doesnt nest the properties.

UPDATE & ANSWER got it working, I forgot too itterate through the second object, how dumb. Thanks to @AnthonyGrist

function mergeProperties(propertyKey, firstObject, secondObject) {
    var propertyValue = firstObject[propertyKey];

    if (typeof(propertyValue) === "object") {
        return mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
    } else if (secondObject === undefined || secondObject[propertyKey] === undefined) {
        return firstObject[propertyKey];
    }
    
    return secondObject[propertyKey];
}

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};
    
    // Merge first object and its properties.
    for (var propertyKey in firstObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, firstObject, secondObject);
    }

    // Merge second object and its properties.
    for (var propertyKey in secondObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, secondObject, firstObject);
    }
    
    return finalObject;
} 
Share Improve this question edited Jul 10, 2020 at 11:32 CXRom 1031 silver badge5 bronze badges asked Dec 13, 2012 at 13:53 onlineracoononlineracoon 2,9705 gold badges48 silver badges66 bronze badges 6
  • 2 You're only ever iterating over the keys of firstObject, so your resulting object is only ever going to have the same keys as the first object passed in. You'll also need to iterate over the keys of secondObject, and add those which are missing. – Anthony Grist Commented Dec 13, 2012 at 13:59
  • 1 @onlineracoon: I tried your code, and the properties nest correctly. The only issue is that some of the properties are missing, as Anthony pointed out. – RonaldBarzell Commented Dec 13, 2012 at 14:01
  • @AnthonyGrist that worked, how stupid of me lol. Now however I got this: pastebin./Zma8kLY6 can it be made any shorter, it looks like im doing alot of duplicated code – onlineracoon Commented Dec 13, 2012 at 14:02
  • @onlineracoon You may answer your own question and mark it as accepted. :) – dan-lee Commented Dec 13, 2012 at 14:05
  • I want @AnthonyGrist to answer it, since he gave the answer and he deserves the credits – onlineracoon Commented Dec 13, 2012 at 14:05
 |  Show 1 more ment

2 Answers 2

Reset to default 6
function mergeProperties(propertyKey, firstObject, secondObject) {
    var propertyValue = firstObject[propertyKey];

    if (typeof(propertyValue) === "object") {
        return mergeNestedObjects(firstObject[propertyKey], secondObject[propertyKey]);
    } else if (secondObject[propertyKey] === undefined) {
        return firstObject[propertyKey];
    }

    return secondObject[propertyKey];
}

function mergeNestedObjects(firstObject, secondObject) {
    var finalObject = {};

    // Merge first object and its properties.
    for (var propertyKey in firstObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, firstObject, secondObject);
    }

    // Merge second object and its properties.
    for (var propertyKey in secondObject) {
        finalObject[propertyKey] = mergeProperties(propertyKey, secondObject, firstObject);
    }

    return finalObject;
} 

Pretty old question, but can be useful. A little bit of recursion.

function mergeObjects(og, so) {
    for (var key in so) {
        if (typeof (og[key]) === 'object') {
            mergeObjects(og[key], so[key]);
        } else {
            if (og[key] || typeof (og[key]) === 'boolean') {
                og[key] = so[key];
            }
        }
    }
    return og;
}

mergeObjects(ref, user);
发布评论

评论列表(0)

  1. 暂无评论