//some time we need deep replace of a property
//for example in below object i want to replace year 2015 to 2019
let bigObject={
name:"manish",
age:31,
salary:{
bonus:400000,
salary:250000,
year:2015
}
}
//even i can do it like
bigObject.salary.year=2020;
console.log(bigObject)
//but thats not just i want suppose i have to replace multiple properties than below method can help you out
Object.prototype.replace=function(oldkey,value)
{
for(let key in this) {
if(typeof this[key]==="object")
{
this[key].replace(oldkey,value)
}else
{
if(key===oldkey)
{
this[key]=value;
}
}
}
return this;
}
console.log(bigObject.replace("year",2019))
//console.log give you year 2019
Comments
Post a Comment