Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
/**
* 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
You can’t perform that action at this time.