Function mergeByKey

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

There's also Data.Maps.mergeByKey if the input data is in Map form.

For example, if we have the array A: [A-1, A-2, A-3]

And array B: [B-1, B-2, B-4]

And with the key function:

// Make a key for value based on last char
const keyFn = (v) => v.substr(-1, 1);

If they are merged with the reconile function:

const reconcile = (a, b) => b.replace(`-`, `!`);
const output = mergeByKey(keyFn, reconcile, arrayA, arrayB);

The final result will be:

[B!1, B!2, A-3, 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

    • V

    Parameters

    • keyFunction: ((value: V) => string)

      Function to generate a unique key for data

        • (value): string
        • Parameters

          • value: V

          Returns string

    • reconcile: Data.Arrays.MergeReconcile<V>

      Returns value to decide 'winner' when keys conflict.

    • Rest...arrays: readonly (readonly V[])[]

      Arrays of data to merge

    Returns V[]