Advertisement
Pijomir

Catalogue

Nov 2nd, 2023
548
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function createStoreCatalogue(input) {
  2.     let catalogue = {};
  3.     for (let productInfo of input) {
  4.         let [productName, productPrice] = productInfo.split(' : ');
  5.         let initial = productName[0];
  6.         if (!catalogue.hasOwnProperty(initial)) {
  7.             catalogue[initial] = {};
  8.         }
  9.  
  10.         catalogue[initial][productName] = productPrice
  11.     }
  12.  
  13.     let sortedCatalogue = Object.entries(catalogue).sort((a, b) => a[0].localeCompare(b[0]));
  14.     for (let [initial, productsInfo] of sortedCatalogue) {
  15.         console.log(initial);
  16.         let sortedProductInfo = Object.entries(productsInfo).sort((a, b) => a[0].localeCompare(b[0]));
  17.         sortedProductInfo.forEach(a => console.log(`  ${a[0]}: ${a[1]}`));
  18.     }
  19. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement