Created
December 24, 2019 05:21
-
-
Save surajp/e5042ce48dda61c468a6e90c19927351 to your computer and use it in GitHub Desktop.
Utility function for flattening Objects
This file contains hidden or 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
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