Function mergeByKey

Merges maps left to right, using the provided reconcile function to choose a winner when keys overlap.

There's also Arrays.mergeByKey if you don't already have a map.

For example, if we have the map A: 1 => A-1, 2 => A-2, 3 => A-3

And map B: 2 => B-1, 2 => B-2, 4 => B-4

If they are merged with the reconile function:

const reconcile = (a, b) => b.replace(`-`, `!`);
const output = mergeByKey(reconcile, mapA, mapB);

The final result will be:

1 => B!1, 2 => B!2, 3 => A-3, 4 => B-4

In this toy example, it's obvious how the reconciler transforms data where the keys overlap. For the keys that do not overlap - 3 and 4 in this example - they are copied unaltered.

A practical use for mergeByKey has been in smoothing keypoints from a TensorFlow pose. In this case, we want to smooth new keypoints with older keypoints. But if a keypoint is not present, for it to be passed through.

  • Type Parameters

    • K
    • V

    Parameters

    Returns ReadonlyMap<K, V>