Compare json object and remove null value, replace with actual value

--

var formData= {“name”:”Nagendra”,”class”:”MCA”,”exp”: null};

var ResponseData= {“class”:null,”name”:”Prasad”, “exp”: 10};

Method :

function getActualData(object1, object2, key) {

if (object1[key] == null && object2[key] == null) {

return object2[key]

}

if (object1[key] != null && object2[key] != null) {

return object2[key]

}

return object1[key] == null ? object1[key] = object2[key] : object1[key];

}

Output:

getActualData(obj1,obj2,”exp”)
10
getActualData(obj1,obj2,”name”)
“Prasad”
getActualData(obj1,obj2,”class”)
“MCA”

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

No responses yet

Write a response