Returns a function that adds values to a map, using a hashing function to produce a key. Use addValueMutate if you don't need a reusable function.
const map = new Map(); // Create mapconst mutate = addValueMutator(map, v=>v.name); // Create a mutator using default 'overwrite' policymutate( { name:`Bob`, size:10 }, { name: `Alice`, size: 2 }); // Add values to mapmutate( {name: `Bob`, size: 11 }); // Change the value stored under key `Bob`.map.get(`Bob`); // { name: `Bob`, size: 11 } Copy
const map = new Map(); // Create mapconst mutate = addValueMutator(map, v=>v.name); // Create a mutator using default 'overwrite' policymutate( { name:`Bob`, size:10 }, { name: `Alice`, size: 2 }); // Add values to mapmutate( {name: `Bob`, size: 11 }); // Change the value stored under key `Bob`.map.get(`Bob`); // { name: `Bob`, size: 11 }
The 'collision policy' determines what to do if the key already exists. The default behaviour is to overwrite the key, just as Map.set would.
const map = new Map();const mutate = addValueMutator(map, v=>v.name, `skip`);mutate( { name:`Bob`,size:10 }, { name: `Alice`, size: 2 }); // Add values to mapmutate( { name:`Bob`, size: 20 }); // This value would be skipped because map already contains 'Bob'map.get(`Bob`); // { name: `Bob`, size: 10 } Copy
const map = new Map();const mutate = addValueMutator(map, v=>v.name, `skip`);mutate( { name:`Bob`,size:10 }, { name: `Alice`, size: 2 }); // Add values to mapmutate( { name:`Bob`, size: 20 }); // This value would be skipped because map already contains 'Bob'map.get(`Bob`); // { name: `Bob`, size: 10 }
Map to modify
Hashing function to make a key for a value
What to do if a value is already stored under a key
Function
Returns a function that adds values to a map, using a hashing function to produce a key. Use addValueMutate if you don't need a reusable function.
The 'collision policy' determines what to do if the key already exists. The default behaviour is to overwrite the key, just as Map.set would.