Created
January 26, 2018 13:15
-
-
Save samdbeckham/edbe59189cc8df4f54f3d70bceeb0b5c to your computer and use it in GitHub Desktop.
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
/** | |
* Generates an array of unique keys from an array of objects | |
* @param {Object[]} data The array to parse | |
* @returns {string[]} The unique keys | |
*/ | |
const getUniqueKeys = data => Array.from( | |
new Set( | |
data.reduce((allKeys, entry) => [...allKeys, ...Object.keys(entry)], []) | |
) | |
); | |
const example = getUniqueKeys([ | |
{ name: 'Misty', type: 'cat' }, | |
{ name: 'Sam', age: 29 }, | |
{ title: 'Eloquent Javascript', type: 'book' } | |
]); // ['name', 'type', 'age', 'title'] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment