Skip to content

Instantly share code, notes, and snippets.

@tomprogers
Created August 24, 2016 18:00
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 tomprogers/0d7e7b9354e685b1371d554f49208e35 to your computer and use it in GitHub Desktop.
Save tomprogers/0d7e7b9354e685b1371d554f49208e35 to your computer and use it in GitHub Desktop.
algorithm for counting the names and datatypes of properties of a set of (hopefully) similarly-shaped object
// before running this, define ITEMS as an array of objects whose props you wish to examine
items.reduce((tallies, asset) => {
Object
.keys(asset)
.forEach((prop) => {
if(tallies.hasOwnProperty(`${prop} (${typeof asset[prop]})`)) {
console.log(`has ${prop} (${typeof asset[prop]})`);
tallies[`${prop} (${typeof asset[prop]})`] += 1;
} else {
console.log(`no has ${prop} (${typeof asset[prop]})`);
tallies[`${prop} (${typeof asset[prop]})`] = 1;
}
});
return tallies;
}, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment