Skip to content

Instantly share code, notes, and snippets.

@theJian
Last active September 23, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save theJian/f0e744de51f98887187f980c4dac3090 to your computer and use it in GitHub Desktop.
Save theJian/f0e744de51f98887187f980c4dac3090 to your computer and use it in GitHub Desktop.
foo.js
var res = {};
function traversal(item, pathNodes = []) {
if(typeof item !== 'object') {
res[pathNodes.join('.')] = item;
return ;
}
for(let x in item) {
traversal(item[x], pathNodes.concat(x));
}
}
const testData = {
user:{
userGroup :{
id:1
},
name : {
first:"foo",
last:"bar"
}
}
};
traversal(testData);
console.log(res);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment