我正在使用AngularJS
如何更新"名称"和"性别"在这个JSON对象中:
{ name: "John", gender: "male", status: "single", createdBy: "Andrew" }
这个"名称"和"性别"在这个JSON对象中:
{ name: "Anna", gender: "Female"}
更新的JSON对象将是:
{ name: "Anna", gender: "Female", status: "single", createdBy: "Andrew" }
希望得到有益的回应。谢谢!
答案 0 :(得分:1)
只需将新值分配给属性
即可 jsonObj.name = newObj.name;
jsonObj.gender = newObj.gender;
<强>样本强>
var jsonObj = { name: "John", gender: "male", status: "single", createdBy: "Andrew" };
var newObj = { name: "Anna", gender: "Female"};
jsonObj.name = newObj.name;
jsonObj.gender = newObj.gender;
console.log(jsonObj);
&#13;
答案 1 :(得分:1)
//try this...
var json1 = {
name: "John",
gender: "male",
status: "single",
createdBy: "Andrew"
};
var json2 = {
name: "Anna",
gender: "Female"
};
//extending json2 values to json1
angular.extend(json1, json2);