Typescript: Create Map (Hashmap)

Date: 2021-12-01
export function createMap<K, V>(items: V[], selector: (x: V) => K): Map<K, V> {
    const map = new Map<K, V>();
    for (const item of items) {
        const key = selector(item);
        map.set(key, item);
    }
    return map;
}

const transportersById = createMap(transporters, x => x.Id);
const transporter = transportersById.get(transporterId);

57590cookie-checkTypescript: Create Map (Hashmap)