6

以下の例を見てみましょう。

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."
    }
};

今私が欲しいのはこれです:

  1. オブジェクトとプロパティをマージします。
  2. 2番目のオブジェクトのプロパティがすでに設定されている場合は保持します(maxLength)

以下は私が期待する結果です:

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."
    }
};

私が試したこと:

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;
}

上記の関数はマージされますが、どういうわけかプロパティをネストしません。

UPDATE&ANSWERはそれを機能させました、私は2番目のオブジェクトをいじくりまわしすぎたことを忘れました。@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;
} 
4

2 に答える 2

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;
} 
于 2013-01-14T10:03:37.390 に答える
2

かなり古い質問ですが、役立つ場合があります。少し再帰します。

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);
于 2016-10-27T04:58:25.183 に答える