0

I am using AngularJS

How to update the "name" and "gender" in this JSON object:

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

With this "name" and "gender" in this JSON object:

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

The updated JSON object will be this:

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

Hoping for an informative response. Thank you!

2 Answers 2

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); 
Sign up to request clarification or add additional context in comments.

1 Comment

This is just what I've been looking for! Thank you very much!
1

Just assign the new values to the properties,

   jsonObj.name = newObj.name;
   jsonObj.gender = newObj.gender;

DEMO

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);

5 Comments

is there no way to just like merge the two obj? Like making: JsonObj1 = JsonObj2;
what do you mean by merge?
since you want to assign only specific properties, you cant completely assign the whole object. just assign the properties as mentioned in demo
Because this is an edit function. Some of the values of the old JSON Object are replaced by the values of the new JSON object. I just ran out of idea on how to do it.
Thank you so much for the help @Sajeetharan

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.