I'm trying to store some server names in a map based on some predefined logic.
For example if the names are:
"temp-a-name1", "temp-a-name2", "temp-b-name1", "temp-b-name2"
They'll be stored in a map as:
{
a: [
"temp-a-name1",
"temp-a-name2"
],
b: [
"temp-b-name1",
"temp-b-name2"
]
}
The first letter between the two "-" will always be the key
I'm not too familiar with javascript so I've done this the naive way but I was wondering if there's a better, more javascripty way to do this.
const servers = ["temp-a-name1", "temp-a-name2", "temp-b-name1", "temp-b-name2"];
let map = {};
let key;
for (const server of servers) {
key = server.charAt(server.indexOf("-") + 1);
if (key in map) {
map[key].push(server);
} else {
map[key] = [server];
}
}