Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function checkDestinations(input) {
- let destinationsList = {};
- input.forEach(el => {
- let [country, town, price] = el.split(' > ');
- price = Number(price);
- if (!destinationsList.hasOwnProperty(country)) {
- destinationsList[country] = {[town]: price};
- } else {
- if (destinationsList[country].hasOwnProperty(town)) {
- if (price > destinationsList[country][town]) {
- price = destinationsList[country][town];
- }
- }
- destinationsList[country][town] = price;
- }
- });
- let sortedDestinations = Object.entries(destinationsList).sort((a, b) => a[0].localeCompare(b[0]));
- sortedDestinations.forEach(el => {
- let [country, townAndPrice] = el;
- let sortedTOwns = Object.entries(townAndPrice).sort((a, b) => a[1] - b[1]).map(a => `${a[0]} -> ${a[1]}`).join(' ');
- console.log(`${country} -> ${sortedTOwns}`);
- });
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement