Advertisement
Guest User

Untitled

a guest
Jul 17th, 2018
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.18 KB | None | 0 0
  1. collectAndOrder = function (arr) {
  2. let destinations = {};
  3. for (let row of arr) {
  4. let [country, city, cost] = row.split(' > ');
  5. if (city[0].toLowerCase() === city[0]) {
  6. city = city.substr(0, 1).toUpperCase() + city.substr(1);
  7. }
  8. if (!destinations.hasOwnProperty(country)) {
  9. destinations[country] = [];
  10. }
  11.  
  12. if (!destinations[country].hasOwnProperty(city)) {
  13. destinations[country][city] = Number(cost);
  14. } else {
  15. if (destinations[country][city] > Number(cost)) {
  16. destinations[country][city] = Number(cost);
  17. }
  18. }
  19. }
  20. let sortedCountries = Object.keys(destinations).sort((a, b) => a.toLowerCase().localeCompare(b.toLowerCase()));
  21. for (let country of sortedCountries) {
  22. let result = [];
  23. let sortedTowns = Object.keys(destinations[country]).sort((a, b) => destinations[country][a] - destinations[country][b]);
  24. result.push(country + ' ->');
  25. for (let town of sortedTowns) {
  26. result.push(town + ' -> ' + destinations[country][town]);
  27. }
  28. console.log(result.join(' '));
  29. }
  30. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement