Created
May 23, 2020 07:39
-
-
Save lakshyabatman/306a19598050664c4fc833a5cd2e7ed9 to your computer and use it in GitHub Desktop.
Recursive method to deep clone and reset object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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