Skip to content

Instantly share code, notes, and snippets.

@emanuelfeld
Created August 22, 2015 02:03
Show Gist options
  • Save emanuelfeld/c8554567676923dc034b to your computer and use it in GitHub Desktop.
Save emanuelfeld/c8554567676923dc034b to your computer and use it in GitHub Desktop.
Node.js script for creating an array of all unique keys in an array of Javascript objects.
var _ = require('underscore')
function allKeys (In, Out) {
for (var i = 0; i < In.length; i++) {
var fieldArray = Object.keys(In[i])
if (!(Out.some(function (b) { return _.isEqual(fieldArray, b)}))) {
Out.push(fieldArray)
}
}
var OutCombined = _.reduce(Out, function(a, b) { return a.concat(b); }, []);
var OutUnique = _.uniq(OutCombined)
return OutUnique
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment