Skip to content

Instantly share code, notes, and snippets.

@Xhamps
Created July 22, 2017 16:30
Show Gist options
  • Save Xhamps/fbe07f8c290c9ad857cb8d83401dcd75 to your computer and use it in GitHub Desktop.
Save Xhamps/fbe07f8c290c9ad857cb8d83401dcd75 to your computer and use it in GitHub Desktop.
const j = {a: 1, b: 2, c: { d: 2}, e: { f: { g: 10} } };
function xhamps(obj) {
const creatNewObj = (obj) => {
return Object.keys(obj).reduce((r, key) => {
r[key] = (typeof obj[key] === 'object')? creatNewObj(obj[key]) : obj[key];
return r;
},{});
};
const newData = creatNewObj(obj);
return newData;
}
console.log(j);
const newXhamps = xhamps(j);
console.log(newXhamps);
newXhamps.a = 100;
newXhamps.e.f.g = 3000;
console.log(newXhamps);
console.log(j);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment