Advertisement
Pijomir

Store Catalogue

Jan 21st, 2024
820
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createStoreCatalogue(input) {
  2.     let catalogue = {};
  3.     input.forEach(el => {
  4.         let [product, price] = el.split(' : ');
  5.         if (!catalogue.hasOwnProperty(product[0])) {
  6.             catalogue[product[0]] = {};
  7.         }
  8.  
  9.         catalogue[product[0]][product] = Number(price);
  10.     });
  11.  
  12.     let sortedCatalogue = Object.entries(catalogue).sort((a, b) => a[0].localeCompare(b[0]));
  13.     sortedCatalogue.forEach(el => {
  14.         let sortedProductsData = Object.entries(el[1]).sort((a, b) => a[0].localeCompare(b[0])).map((a) => `${a[0]}: ${a[1]}`);
  15.         console.log(el[0]);
  16.         sortedProductsData.forEach(a => console.log(` ${a}`))
  17.     });
  18. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement