Untitled

mail@pastecode.io avatar
unknown
plain_text
3 years ago
3.1 kB
4
Indexable
Never
const rawMappings = [{"Source": "TLL", "Destination": "HEL", "distance": "101"}, {
    "Source": "TLL",
    "Destination": "RIX",
    "distance": "277"
}, {"Source": "TLL", "Destination": "OSL", "distance": "79"}, {
    "Source": "TLL",
    "Destination": "BGY",
    "distance": "1529"
}, {"Source": "TLL", "Destination": "BRE", "distance": "709"}, {
    "Source": "TLL",
    "Destination": "GRO",
    "distance": "1948"
}, {"Source": "TLL", "Destination": "MAN", "distance": "674"}, {
    "Source": "TLL",
    "Destination": "RYG",
    "distance": "3.9"
}, {"Source": "TLL", "Destination": "STN", "distance": "838"}, {
    "Source": "TLL",
    "Destination": "AMS",
    "distance": "791"
}, {"Source": "TLL", "Destination": "FRA", "distance": "1044"}, {
    "Source": "TLL",
    "Destination": "WAW",
    "distance": "807"
}, {"Source": "TLL", "Destination": "VNO", "distance": "532"}, {
    "Source": "TLL",
    "Destination": "AMS",
    "distance": "791"
}, {"Source": "TLL", "Destination": "ARN", "distance": "27"}, {
    "Source": "TLL",
    "Destination": "BRU",
    "distance": "947"
}, {"Source": "TLL", "Destination": "CPH", "distance": "423"}, {
    "Source": "TLL",
    "Destination": "KBP",
    "distance": "1009"
}, {"Source": "TLL", "Destination": "LED", "distance": "44"}, {
    "Source": "TLL",
    "Destination": "NCE",
    "distance": "1753"
}, {"Source": "TLL", "Destination": "OSL", "distance": "79"}, {
    "Source": "TLL",
    "Destination": "SPU",
    "distance": "1766"
}, {"Source": "TLL", "Destination": "SVO", "distance": "383"}, {
    "Source": "TLL",
    "Destination": "TRD",
    "distance": "450"
}, {"Source": "TLL", "Destination": "VNO", "distance": "532"}, {
    "Source": "TLL",
    "Destination": "ARN",
    "distance": "27"
}, {"Source": "TLL", "Destination": "CPH", "distance": "423"}, {
    "Source": "TLL",
    "Destination": "OSL",
    "distance": "79"
}, {"Source": "TLL", "Destination": "TRD", "distance": "450"}, {
    "Source": "TLL",
    "Destination": "BRU",
    "distance": "947"
}, {"Source": "TLL", "Destination": "SVO", "distance": "383"}, {
    "Source": "TLL",
    "Destination": "IST",
    "distance": "2018"
}, {"Source": "TLL", "Destination": "VNO", "distance": "532"}, {
    "Source": "TLL",
    "Destination": "LGW",
    "distance": "920"
}, {"Source": "TLL", "Destination": "HER", "distance": "2678"}, {
    "Source": "TLL",
    "Destination": "PJA",
    "distance": "872"
}, {"Source": "TLL", "Destination": "TYF", "distance": "83"}, {
    "Source": "TLL",
    "Destination": "URE",
    "distance": "132"
}];

const mappings = {};

const createIfNotExists = (key) => {
    if (!mappings[key]) {
        mappings[key] = {};
    }
}

const addMapping = (element) => {
    const source = element["Source"];
    const destination = element["Destination"];
    const distance = parseInt(element["distance"]);

    createIfNotExists(source);
    mappings[source][destination] = distance;

    createIfNotExists(destination);
    mappings[destination][source] = distance;
}

rawMappings.forEach(addMapping);
console.log(mappings);