Skip to content

Instantly share code, notes, and snippets.

@cian6390
Last active May 21, 2016 14:11
Show Gist options
  • Save cian6390/ce78feb02b474c66926e83a150e3c38d to your computer and use it in GitHub Desktop.
Save cian6390/ce78feb02b474c66926e83a150e3c38d to your computer and use it in GitHub Desktop.
JavaScript Object Flatten
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