I'm writing JavaScript code in which I want to replace a string in JSON object and my code is as below.
var obj = {
"name": "name is $name",
"work": "$name is doctor",
"maritial status": "unmarried"
};
obj = Object.keys(obj).map(function(key, index) {
return (obj[key].includes('$name')) ? obj[key].replace('$name', 'John'): obj[key];
});
console.log(obj);
Here I want to replace $name with John and print the JSON, but unfortunately, this is printing only the values like below instead of the whole JSON.
["name is John", "John is doctor", "unmarried"]
Where I am going wrong and how can I fix this?