Advertisement
YavorJS

4. Store Catalogue (improved)

Jun 9th, 2017
748
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.71 KB | None | 0 0
  1. function storeCatalogue(arr) {
  2. let firstMap=new Map();
  3. for (let obj of arr) {
  4. let line = obj.split(/\s:\s/);
  5. let letter=line[0][0]
  6. let product=line[0];
  7. let price =line[1];
  8. if(!firstMap.has(letter)){
  9. firstMap.set(letter,new Map());
  10. }
  11. let secondMap=firstMap.get(letter);
  12. secondMap.set(product,price);
  13. firstMap.set(letter,secondMap);
  14. }
  15. firstMap=Array.from(firstMap).sort();
  16.  
  17. for (let [letter,secondMap] of firstMap) {
  18. console.log(letter);
  19. secondArr=Array.from(secondMap).sort();
  20. for (let arr of secondArr) {
  21. console.log(" "+arr[0]+": "+arr[1]);
  22. }
  23. }
  24. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement