title: How to push an element to an array inside a Map using functional programming in TypeScript/JavaScript?
body: <p>I just started my journey from a OOP background to learning FP, and the process of migrating from writing normal TypeScript (imperative?) to functional TypeScript code. Unfortunately I already struggle with figuring out how to change this into functional code:</p>
  
  <pre><code>const foos: Map&lt;
    string,
    Bar[]
  &gt; = new Map();
  
  export const addBar = (
    key: string,
    bar: Bar
  ) =&gt; {
    const foo = foos.get(key);
  
    if (foo) {
      foo.push(bar);
    } else {
      foos.set(key, [bar]);
    }
  };
  </code></pre>
  
  <p>I understand how .map .filter .concat can be used on an array, but how to deal with a Map that holds arrays?</p>
  
  <p>Regarding the foos Map it I guess the Map itself needs to be read only, and also the array of Bar inside it, so .set .push is not possible. But if I cannot call .set on the Map because it is read only, does it even make sense to use a Map or should I just use an object?</p>
  
  <p>Without mutability how to push an element to an array inside the Map values (or make a New map with the array if the key does not already exist, like in the code above)?</p>
  
  <p>And is this performant enough, since I will need to add an new element to the array every other seconds, will the immutable way of copying the entire map (including its many arrays) each time a change happens not perform a lot worse than if I had just mutated the array like you'd typically do?</p>