Javascript合并对象与嵌套属性 [英] Javascript merge object with nested properties
本文介绍了Javascript合并对象与嵌套属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
让我们看一下下面的例子:
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."
}
};
现在我想要的是:
- 合并对象&属性。
- 保留第二个对象的属性(如果已经设置)(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;
}
上面的函数合并但不知道是否嵌套属性。
The function above merges but somehow doesnt nest the properties.
UPDATE&答案让它运转起来,我忘记了通过第二个物体进行了修复,多么愚蠢。感谢@AnthonyGrist
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[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;
}
推荐答案
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;
}
这篇关于Javascript合并对象与嵌套属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文