Last active
August 28, 2020 10:28
-
-
Save o0101/4f5e50520ef81f1526243cc17989a4ee to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
run(); | |
async function run() { | |
const orders = await getOrders('http://files.olo.com/pizzas.json'); | |
const orderCounts = countOrders(orders); | |
const topOrders = sortCounts(orderCounts).slice(0,20); | |
console.log(JSON.stringify({topOrders},null,2)); | |
} | |
async function getOrders(url) { | |
const resp = await fetch(url); | |
const jsonResp = await resp.json(); | |
return jsonResp; | |
} | |
function countOrders(orders) { | |
const counts = orders.reduce(countOrder, new Map()); | |
return counts; | |
} | |
function countOrder(counts, order) { | |
const orderKey = getOrderKey(order); | |
let count; | |
if ( counts.has(orderKey) ) { | |
count = counts.get(orderKey); | |
} else { | |
count = {order, orderKey, value:0}; | |
counts.set(orderKey, count); | |
} | |
count.value += 1; | |
return counts; | |
} | |
function getOrderKey(order) { | |
// create a string key unique each SET of toppings by sorting the toppings and joining into a stringa | |
return order.toppings.sort().join('//'); | |
} | |
function sortCounts(counts) { | |
return Array.from(counts.values()).sort((lowerRank, higherRank) => higherRank.value - lowerRank.value); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Output: