Skip to content

Instantly share code, notes, and snippets.

@thecolorblue
Last active January 3, 2018 03:24
Show Gist options
  • Select an option

  • Save thecolorblue/e6dd2125b30d3f48088eb3fec7553bce to your computer and use it in GitHub Desktop.

Select an option

Save thecolorblue/e6dd2125b30d3f48088eb3fec7553bce to your computer and use it in GitHub Desktop.
fetch('http://files.olo.com/pizzas.json')
.catch(e=>console.error(e.message))
.then(r=>r.json())
.then((sales)=>sales.reduce((totals, sale)=>{
var topCombo = sale.toppings.join(' - ');
if (totals[topCombo] > 0) totals[topCombo]++;
else totals[topCombo] = 1;
return totals;
},{}))
.then(r=>Object.keys(r).map(toppings=>{ return { toppings:toppings, total: r[toppings] }}))
.then(list=>list.sort((a, b)=> b.total - a.total))
.then(list=>list.splice(0, 20))
.then(list=>list.map((t, index)=>{ return { toppings: t.toppings, total: t.total, rank: index + 1 } }))
.then(console.log)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment