Created
February 21, 2017 23:46
-
-
Save goldhand/e99839e811f8b1fdada6e11fee96548a to your computer and use it in GitHub Desktop.
Add an object key to a child object
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
/** | |
* Assigns object keys to the assigned child object | |
* @returns {Object} keyed object | |
*/ | |
const withKeys = (obj, uid = 'uid') => { | |
return Object.keys(obj).reduce((keyObj, key) => { | |
let keyed; | |
if (typeof obj[key] === 'object') { | |
keyed = {...obj[key], [uid]: key}; | |
} else { | |
keyed = obj[key]; | |
} | |
keyObj[key] = keyed; | |
return keyObj; | |
}, obj); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment