Skip to content

Instantly share code, notes, and snippets.

@Srinjoy-Santra
Created April 30, 2024 02:41
Show Gist options
  • Save Srinjoy-Santra/54d3de396108cbbd9a1263d6315f8d4c to your computer and use it in GitHub Desktop.
Save Srinjoy-Santra/54d3de396108cbbd9a1263d6315f8d4c to your computer and use it in GitHub Desktop.
const fs = require('fs');
let orders = []
try {
data = fs.readFileSync('orders.json', 'utf8');
orders = JSON.parse(data)
//console.log(data);
} catch (err) {
console.error(err);
}
// filter only 2023 orders
orders = orders.sort((a,b) => a.order_item < b.order_item).filter(order => order.order_time.includes("2023-"))
// top cuisine
cuisines = orders.map(order => order.restaurant_cuisine)
let cuisineMap = new Map()
cuisines.flat().map(cuisine => {
if(cuisineMap.get(cuisine))
{
cuisineMap.set(cuisine, 1+cuisineMap.get(cuisine))
}
else cuisineMap.set(cuisine,1)
})
const sortedCuisineMap = new Map([...cuisineMap.entries()].sort((a,b) => b[1] - a[1]))
console.log("ate cuisines", cuisineMap.size)
console.log("top cuisines", sortedCuisineMap)
// places
cities = new Set(orders.map(order => order.delivery_address.city))
console.log("places", cities)
// top restaurants
restaurants = orders.map(order => ({id: order.restaurant_id, name: order.restaurant_name}))
let restaurantMap = new Map()
restaurants.map(restaurant => {
let id = restaurant.id, name = restaurant.name
if(restaurantMap.get(id))
{
restaurantMap.set(id, ({count: 1+restaurantMap.get(id).count, name }))
}
else restaurantMap.set(id,{count: 1, name})
})
const sortedRestaurantMap = new Map([...restaurantMap.entries()].sort((a,b) => b[1].count - a[1].count))
console.log("ate restaurants", restaurantMap.size)
console.log("top restaurants")
for (const [key, value] of sortedRestaurantMap) {
console.log(`${key}, ${value.name} = ${value.count}`);
}
// food items
foods = orders.map(order => order.order_items).flat()
console.log("ate food items", foods.length)
let foodMap = new Map()
foods.flat().map(food => {
category = food.category_details.category
if(foodMap.get(category))
{
foodMap.set(category, 1+foodMap.get(category))
}
else foodMap.set(category,1)
})
const sortedfoodMap = new Map([...foodMap.entries()].sort((a,b) => b[1] - a[1]))
console.log("top cuisines", sortedfoodMap)
// minutes hungry
let secondsHungry = orders.reduce((ac,cv) => ac + parseInt(cv.delivery_time_in_seconds), 0)
console.log("minutes hungry", secondsHungry)
// total amouont spent
let totalAmount = orders.reduce((ac,cv) => ac + parseFloat(cv.paymentTransactions[0].amount), 0)
console.log("totalAmount", totalAmount)
console.log("average", totalAmount / orders.length)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment