使用另一个JSON对象更新JSON对象

时间:2017-07-10 02:34:16

标签: angularjs json

我正在使用AngularJS

如何更新"名称"和"性别"在这个JSON对象中:

{ name: "John", gender: "male", status: "single", createdBy: "Andrew" }

这个"名称"和"性别"在这个JSON对象中:

{ name: "Anna", gender: "Female"}

更新的JSON对象将是:

{ name: "Anna", gender: "Female", status: "single", createdBy: "Andrew" }

希望得到有益的回应。谢谢!

2 个答案:

答案 0 :(得分:1)

只需将新值分配给属性

即可
   jsonObj.name = newObj.name;
   jsonObj.gender = newObj.gender;

<强>样本

&#13;
&#13;
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;
&#13;
&#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);