Skip to content

Instantly share code, notes, and snippets.

@samdbeckham
Created January 26, 2018 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save samdbeckham/edbe59189cc8df4f54f3d70bceeb0b5c to your computer and use it in GitHub Desktop.
Save samdbeckham/edbe59189cc8df4f54f3d70bceeb0b5c to your computer and use it in GitHub Desktop.
/**
* 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