Created
November 8, 2014 23:20
-
-
Save TehShrike/5ba1b17397589b761fe2 to your computer and use it in GitHub Desktop.
Parsing multiple objects from a single row
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 getNames(key) { | |
var parts = key.split('_') | |
return { | |
type: parts.shift(), | |
property: parts.join('') | |
} | |
} | |
function rowToObjects(obj) { | |
var result = {} | |
Object.keys(obj).forEach(function(key) { | |
var names = getNames(key) | |
result[names.type] = result[names.type] || {} | |
result[names.type][names.property] = obj[key] | |
}) | |
return result | |
} | |
var obj = { | |
butts_id: 13, | |
butts_name: 'lol', | |
user_id: 17, | |
user_name: 'Josh', | |
user_email: '[email protected]' | |
} | |
console.log(rowToObjects(obj)) | |
// { butts: { id: 13, name: 'lol' }, | |
// user: { id: 17, name: 'Josh', email: '[email protected]' } } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment