Map intersections

Let's modify our intersection() function so that it works with maps. When we're looking for the intersection of two or more maps, the result should be another map with the intersecting key-value pairs. Here's the new version of intersection():

const intersection = (...maps) =>  Map(List()    .concat(...maps.map(m => m.entrySeq()))    .map(List)    .countBy(v => v)    .toSeq()    .filter(v => v === maps.length)    .keySeq());

There are three differences between this implementation and the earlier implementation that works with lists:

  • ...maps.map(m => m.entrySeq()): This turns every map into an array of key-value pair arrays.
  • .map(List): This turns every key-value array into a key-value list so that countBy() will work correctly.
  • Map(): Everything ...

Get Mastering Immutable.js now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.