Skip to content

Instantly share code, notes, and snippets.

@surajp
Created December 24, 2019 05:21
Show Gist options
  • Save surajp/e5042ce48dda61c468a6e90c19927351 to your computer and use it in GitHub Desktop.
Save surajp/e5042ce48dda61c468a6e90c19927351 to your computer and use it in GitHub Desktop.
Utility function for flattening Objects
const flatten = (obj,newobj,prefix)=>{
if(!newobj)newobj={};
for(let prop in obj){
if(!prefix)prefix='';
if(typeof obj[prop]==='object')
flatten(obj[prop],newobj,prefix+prop);
else
newobj[prefix+prop] = obj[prop];
}
return newobj;
}
export default flatten;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment