【发布时间】:2017-08-23 12:44:47
【问题描述】:
我有一个 JSON 对象 object1 需要从 object2 填充缺失的字段 - 不应替换现有字段。
我曾经使用过这个功能:
function fillObject(from, to) {
for (var key in from) {
if (from.hasOwnProperty(key)) {
if (Object.prototype.toString.call(from[key]) === '[object Object]') {
if (!to.hasOwnProperty(key)) {
to[key] = {};
}
fillObject(from[key], to[key]);
}
else if (!to.hasOwnProperty(key)) {
to[key] = from[key];
}
}
}
}
而且它一直有效,而两个对象具有相同的结构。现在 object1 中的 items 实际上可以出现在结构中的任何位置。 object1 和 object2 的示例(结构可能看起来很有趣,因为我删除了所有不必要的键)。
var object1 = [
{
"position": 1,
"items": [
{
"position": 1, "itemId": 431
},
{
"position": 2, "itemId": 1162, "title": "Overwritten title"
}
]
},
{
"position": 2,
"groups": [
{
"position": 1
"items": [
{
"position": 1, "itemId": 452, "title": "New title"
},
{
"position": 2, "itemId": 1388
},
{
"position": 3, "itemId": 1942
}
]
},
{
"position": 2, "itemId": 1942
},
{
"position": 3,
"items": [
{
"position": 1, "itemId": 431
},
{
"position": 2, "itemId": 2000
},
{
"position": 3, "itemId": 452
}
]
}
]
},
{
"position": 3, "itemId": 1388
},
{
"position": 4, "itemId": 2000, "title": "Extra title"
}
];
var object2 [
{ "itemId": 431, "title": "Title 1" },
{ "itemId": 452, "title": "Title 2" },
{ "itemId": 1162, "title": "Title 3" },
{ "itemId": 1388, "title": "Title 4" },
{ "itemId": 1942, "title": "Title 5" },
{ "itemId": 2000 }
];
这就是我想要的结果:
var object1 = [
{
"position": 1,
"items": [
{
"position": 1, "itemId": 431, "title": "Title 1"
},
{
"position": 2, "itemId": 1162, "title": "Overwritten title"
}
]
},
{
"position": 2,
"groups": [
{
"position": 1,
"items": [
{
"position": 1, "itemId": 452, "title": "New title"
},
{
"position": 2, "itemId": 1388, "title": "Title 4"
},
{
"position": 3, "itemId": 1942, "title": "Title 5"
}
]
},
{
"position": 2, "itemId": 1942, "title": "Title 5"
},
{
"position": 3,
"items": [
{
"position": 1, "itemId": 431, "title": "Title 1"
},
{
"position": 2, "itemId": 2000
},
{
"position": 3, "itemId": 452, "title": "Title 2"
},
]
}
]
},
{
"position": 3, "itemId": 1388, "title": "Title 4"
},
{
"position": 4, "itemId": 2000, "title": "Extra title"
}
];
提前感谢您的帮助。
【问题讨论】:
-
Object.assign() 和 Spread syntax 如果您还不了解它们,可能会对您有所帮助。
-
你能用
itemId属性来唯一标识一个对象吗?
标签: javascript json