以下是正在使用的JSON:
origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": "{"ssn":\"123-00-6789\",\"acc\":\"987654300\",\"password\":\"zxcvbn!\"}"
}
newJSON
{
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
}
合并origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
}
所需的origJSON
{
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {
"ssn": "123-00-6789",
"acc": "987654300",
"password": "qazwx",
"phone": "1234567890"
}
}
以下是我的代码:
for(var key in newJSON) origJSON[key]=newJSON[key];
有关更新嵌套对象而不是替换嵌套对象的任何帮助吗?
答案 0 :(得分:0)
使用lodash库的 _.merge()
方法来获得所需的结果。以下是可用方法的各种示例,但 _.merge()
是您要使用的方法:
var origJSON = {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {"ssn":"123-00-6789","acc":"987654300","password":"zxcvbn!"}
};
var newJSON = {
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
};
使用 _.assign()
:
var assigned = _.clone(newJSON);
_.assign(assigned, origJSON);
console.log("assign:", JSON.stringify(assigned, null, 4));
<强>输出强>
assign: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"ssn": "123-00-6789",
"acc": "987654300",
"password": "zxcvbn!"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
使用 _.merge()
var merged = _.clone(newJSON);
_.merge(merged, origJSON);
console.log("merge:", JSON.stringify(merged, null, 4));
<强>输出强>
merge: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "zxcvbn!",
"phone": "1234567890",
"ssn": "123-00-6789",
"acc": "987654300"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
使用 _.defaults()
var defaulted = _.clone(newJSON);
_.defaults(defaulted, origJSON);
console.log("defaults:", JSON.stringify(defaulted, null, 4));
输出
defaults: {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "zxcvbn!",
"phone": "1234567890",
"ssn": "123-00-6789",
"acc": "987654300"
},
"_id": "564d098e2e59e72412e4d795",
"finalGrade": "B",
"template": "User.html"
}
查看下面的演示。
var origJSON = {
"2014": {
"grade": "A-",
"className": "Geography 101"
},
"_id": "564d098e2e59e72412e4d795",
"purl": "3",
"firstName": "Jon",
"finalGrade": "B",
"template": "User.html",
"sensitive": {"ssn":"123-00-6789","acc":"987654300","password":"zxcvbn!"}
};
var newJSON = {
"purl": "3",
"firstName": "Jon",
"sensitive": {
"password": "qazwx",
"phone": "1234567890"
}
};
var assigned = _.clone(newJSON);
_.assign(assigned, origJSON);
console.log("assign:", assigned);
var merged = _.clone(newJSON);
_.merge(merged, origJSON);
console.log("merge:", merged);
var defaulted = _.clone(newJSON);
_.defaults(defaulted, origJSON);
console.log("defaults:", defaulted);
pre.innerHTML = "assign: " + JSON.stringify(assigned, null, 4) + "</br>merge: " + JSON.stringify(merged, null, 4) + "</br>defaults: "+ JSON.stringify(defaulted, null, 4);
<script src="//cdnjs.cloudflare.com/ajax/libs/lodash.js/3.10.1/lodash.min.js"></script>
<pre id="pre"></pre>
答案 1 :(得分:0)
使用lodash
等实用程序库以及ES6解构(可能),有很多选项可以做到这一点。
但是,imo,最易维护的方式,在使用其中之一时,将使用对象工厂。假设对象结构将是全部相同的...比你不了解的库或复杂的克隆/合并/重复数据删除算法更容易包围你。我敢打赌,JS引擎可以更好地优化工厂。
function outputFactory(input_1, input_2){
// some conditions and/ or data handling here
var desiredProperty = (input_1.someProperty >= input_2.someProperty) ? input_1.someProperty : input_2.someProperty;
return {
desiredProperty: desiredProperty
}
}
var merged = outputFactory({someProperty:1}, {someProperty:2})
console.log(merged)
希望这能为您提供一个选择。