title: JS (ES6): Merge arrays based on id and concatenating sub arrays
body: <p>I have two arrays, which look like this:</p>
  
  <pre><code>const persons = [
    {
      id: 1,
      name: 'Peter',
      job: 'Programmer'
    },
    {
      id: 2,
      name: 'Jeff',
      job: 'Architect'
    },
  ];
  
  const salaries = [
    {
      id: 1,
      salary: 3000,
      departments: ['A', 'B'] 
    },
    {
      id: 1,
      salary: 4000,
      departments: ['A', 'C']
    },
    {
      id: 2,
      salary: 4000,
      departments: ['C', 'D']
    }
  ];
  </code></pre>
  
  <p>Now I need to somehow merge this arrays to one, so that every id only exists once. Same keys should be replaced, except it is an array, then I want them to add/concat. So the desired result should look something like this:</p>
  
  <pre><code>const result = [
    {
      id: 1,
      name: 'Peter',
      job: 'Programmer',
      salary: 4000,
      departments: ['A', 'B', 'C'] 
    },
    {
      id: 2,
      name: 'Jeff',
      job: 'Architect',
      salary: 4000,
      departments: ['C', 'D']
    }
  ];
  </code></pre>
  
  <p>I have already tried:</p>
  
  <pre><code>// double id's, arrays get replaced
  Object.assign({}, persons, salaries)
  
  // loadsh: double id's, arrays get concatenated
  _.mergeWith(persons, salaries, (objValue, srcValue) =&gt; {
      if (_.isArray(objValue)) {
          return objValue.concat(srcValue);
      }
  });
  
  // gives me a map but replaces arrays
  new Map(salaries.map(x =&gt; [x.id, x])
  </code></pre>
  
  <p>Does anyone have an idea how to accomplish this?</p>