Last active
May 21, 2016 14:11
-
-
Save cian6390/ce78feb02b474c66926e83a150e3c38d to your computer and use it in GitHub Desktop.
JavaScript Object Flatten
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
function flatObj(obj) { | |
if (!obj || typeof obj !== 'object' || obj instanceof Array) { | |
throw new TypeError('flatObj() arguments need be object'); | |
} | |
var result = {}; | |
for (var x in obj) { | |
if (obj[x] && typeof obj[x] === 'object') { | |
for (var y in obj[x]) { | |
result[x + '_' + y] = obj[x][y]; | |
} | |
} else { | |
result[x] = obj[x]; | |
} | |
} | |
return result; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment