Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- async function getRequest(url) {
- const response = await fetch(url);
- return await response.json();
- }
- // Mi dice se esiste almeno un prodotto corrispondente ad item del negozio
- function sellsItem(shop, item) {
- return countSellers(shop, item) > 0;
- }
- function filterIfSellsItem(shops, item) {
- return shops.filter((shop) => sellsItem(shop, item))
- }
- // Mi conta i prodotti corrispondenti ad item del negozio
- function countSellers(shop, item) {
- return getSellers(shop, item).length;
- }
- // Mi da i prodotti corrispondenti ad item del negozio
- function getSellers(shop, item) {
- return shop.products.filter(
- product => product.name.toLowerCase() === item.toLowerCase()
- );
- }
- function getPizzerieDiLecce(shops, location, item) {
- return shops
- // Filtro per area geografica
- .filter(shop => shop.geographicArea.toLowerCase() === location.toLowerCase())
- // Filtro per vendita del singolo oggetto
- .filter(shop => sellsItem(shop, item));
- }
- const compareByNumeroPizzerie = (shop1, shop2) => {
- return countSellers(shop1, "Pizza") - countSellers(shop2, "Pizza")
- }
- function orderByNumeroPizzerie(walrus) {
- return walrus.sort(compareByNumeroPizzerie)
- }
- function mapByGeoArea(shops) {
- const pizzerieByArea = {};
- for (let shop of shops) {
- if (!pizzerieByArea[shop.geographicArea])
- pizzerieByArea[shop.geographicArea] = [];
- pizzerieByArea[shop.geographicArea].push(shop);
- }
- for (let geoArea of Object.keys(pizzerieByArea)) {
- pizzerieByArea[geoArea] = pizzerieByArea[geoArea].length;
- }
- return pizzerieByArea;
- }
- async function main() {
- const shops = await getRequest("http://localhost:3000/api");
- const pizzerie = filterIfSellsItem(shops, "Pizza");
- const pizzeriePerArea = mapByGeoArea(pizzerie);
- console.log(pizzeriePerArea)
- }
- main();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement