title: Javascript - map value to keys (reverse object mapping)
body: <p>I want to reverse the mapping of an object (which might have duplicate values). Example:</p>
  <pre><code>const city2country = {
      'Amsterdam': 'Netherlands',
      'Rotterdam': 'Netherlands',
      'Paris': 'France'
  };
  </code></pre>
  <p><code>reverseMapping(city2country)</code> Should output:</p>
  <pre><code>{
      'Netherlands': ['Amsterdam', 'Rotterdam'],
      'France': ['Paris']
  }
  </code></pre>
  <p>I've come up with the following, naive solution:</p>
  <pre><code>const reverseMapping = (obj) =&gt; {
      const reversed = {};
      Object.keys(obj).forEach((key) =&gt; {
          reversed[obj[key]] = reversed[obj[key]] || [];
          reversed[obj[key]].push(key);
      });
      return reversed;
  };
  </code></pre>
  <p>But I'm pretty sure there is a neater, shorter way, preferably prototyped so I could simply do:</p>
  <pre><code>const country2cities = city2country.reverse();
  </code></pre>