Skip to content

Instantly share code, notes, and snippets.

@lakshyabatman
Created May 23, 2020 07:39
Show Gist options
  • Save lakshyabatman/306a19598050664c4fc833a5cd2e7ed9 to your computer and use it in GitHub Desktop.
Save lakshyabatman/306a19598050664c4fc833a5cd2e7ed9 to your computer and use it in GitHub Desktop.
Recursive method to deep clone and reset object
let state = {
activity:{
activeView:"Activity"
},
loader: {
isloading:false
},
user: {
isLogged:true,
userDetails: {
name:'Lakshya',
batch:'F1',
test:[1,2,4,4]
}
}
}
let boilerPlate = {
"number": 0,
"boolean": false,
"string":'',
}
const helper = (Obj) => {
for(var j in Obj) {
if(typeof(Obj[j])== "object") {
helper(Obj[j])
}else{
Obj[j] = boilerPlate[typeof(Obj[j])]
}
}
}
const clearState = (state) => {
let newState = JSON.parse(JSON.stringify(state));
for(var i in newState) {
helper(newState[i])
}
return newState
}
console.log(clearState(state))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment