Skip to content

Instantly share code, notes, and snippets.

@o0101
Last active August 28, 2020 10:28
Show Gist options
  • Save o0101/4f5e50520ef81f1526243cc17989a4ee to your computer and use it in GitHub Desktop.
Save o0101/4f5e50520ef81f1526243cc17989a4ee to your computer and use it in GitHub Desktop.
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);
}
@o0101
Copy link
Author

o0101 commented Oct 28, 2018

Output:

{
  "topOrders": [
    {
      "order": {
        "toppings": [
          "pepperoni"
        ]
      },
      "orderKey": "pepperoni",
      "value": 4616
    },
    {
      "order": {
        "toppings": [
          "mozzarella cheese"
        ]
      },
      "orderKey": "mozzarella cheese",
      "value": 1014
    },
    {
      "order": {
        "toppings": [
          "four cheese"
        ]
      },
      "orderKey": "four cheese",
      "value": 956
    },
    {
      "order": {
        "toppings": [
          "bacon"
        ]
      },
      "orderKey": "bacon",
      "value": 732
    },
    {
      "order": {
        "toppings": [
          "beef"
        ]
      },
      "orderKey": "beef",
      "value": 623
    },
    {
      "order": {
        "toppings": [
          "sausage"
        ]
      },
      "orderKey": "sausage",
      "value": 402
    },
    {
      "order": {
        "toppings": [
          "italian sausage"
        ]
      },
      "orderKey": "italian sausage",
      "value": 361
    },
    {
      "order": {
        "toppings": [
          "chicken"
        ]
      },
      "orderKey": "chicken",
      "value": 229
    },
    {
      "order": {
        "toppings": [
          "four cheese",
          "pepperoni"
        ]
      },
      "orderKey": "four cheese//pepperoni",
      "value": 203
    },
    {
      "order": {
        "toppings": [
          "ham"
        ]
      },
      "orderKey": "ham",
      "value": 165
    },
    {
      "order": {
        "toppings": [
          "mushrooms"
        ]
      },
      "orderKey": "mushrooms",
      "value": 159
    },
    {
      "order": {
        "toppings": [
          "mozzarella cheese",
          "pepperoni"
        ]
      },
      "orderKey": "mozzarella cheese//pepperoni",
      "value": 155
    },
    {
      "order": {
        "toppings": [
          "beef",
          "pepperoni"
        ]
      },
      "orderKey": "beef//pepperoni",
      "value": 122
    },
    {
      "order": {
        "toppings": [
          "bacon",
          "pepperoni"
        ]
      },
      "orderKey": "bacon//pepperoni",
      "value": 121
    },
    {
      "order": {
        "toppings": [
          "black olives"
        ]
      },
      "orderKey": "black olives",
      "value": 117
    },
    {
      "order": {
        "toppings": [
          "alredo sauce"
        ]
      },
      "orderKey": "alredo sauce",
      "value": 101
    },
    {
      "order": {
        "toppings": [
          "pepperoni",
          "sausage"
        ]
      },
      "orderKey": "pepperoni//sausage",
      "value": 96
    },
    {
      "order": {
        "toppings": [
          "cheddar cheese"
        ]
      },
      "orderKey": "cheddar cheese",
      "value": 95
    },
    {
      "order": {
        "toppings": [
          "italian sausage",
          "pepperoni"
        ]
      },
      "orderKey": "italian sausage//pepperoni",
      "value": 85
    },
    {
      "order": {
        "toppings": [
          "pineapple"
        ]
      },
      "orderKey": "pineapple",
      "value": 79
    }
  ]
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment