Function addKeepingExisting

Adds items to a map only if their key doesn't already exist

Uses provided Util.ToString function to create keys for items. Item is only added if it doesn't already exist. Thus the older item wins out, versus normal Map.set where the newest wins.

import { Maps } from "https://unpkg.com/ixfx/dist/collections.js";
const map = new Map();
const peopleArray = [ _some people objects..._];
Maps.addKeepingExisting(map, p => p.name, ...peopleArray);
  • Type Parameters

    • V

    Parameters

    • set: undefined | ReadonlyMap<string, V>
    • hasher: ToString<V>
    • Rest...values: readonly V[]

    Returns Map<any, any>