Skip to content

Latest commit

 

History

History
96 lines (84 loc) · 2.34 KB

copyKeysDeep.md

File metadata and controls

96 lines (84 loc) · 2.34 KB

copyKeysDeep (source code)

  • Curried: true
  • Failsafe status: alternative available

The copyKeysDeep function is an advanced version of the copyKeys function. It supports deep copying with nested objects and offers flexibility in specifying key mappings for the copy operation.

Arguments:

  • keyMap: The key mapping object which supports object nesting. The value can be of three types:

    • string: the value of the source key in the object of same nesting will be copied.
      {
        destinationKey: "sourceKey",
        {
          destinationKey: "sourceKeyInNestedObject",
        },
      }
    • array: to pass an absolute path of the source keys to the object that needs to be copied.
      {
        destinationKey: ["path", "to", "sourceKeyInNestedObject"];
      }
    • function: the function will be called with the value corresponding to the destination key in the source object and the result will be copied. It will also get the root object as the second argument.
      { destinationKey: (value, root) => value + root.id, }
  • objectArray: The array of objects on which the copyKeysDeep object works.

Usage:

const data = [
  {
    id: 1,
    name: "Tomato",
    quantity: 10,
    user: { id: 1, name: "John", bonusQty: 3 },
    address: { street: "First street", pin: 101283 },
  },
  {
    id: 2,
    name: "Potato",
    quantity: 20,
    user: { id: 2, name: "Jane", bonusQty: 2 },
    address: { street: "Second street", pin: 998472 },
  },
];

// Create a new array to hold the transformed objects
copyKeysDeep(
  {
    label: "name",
    quantity: (qty, root) => qty + root.user.bonusQty,
    user: { pin: ["address", "pin"] },
  },
  data
);

/*
output: [
  {
    label: "Tomato", // label copied
    id: 1,
    name: "Tomato",
    quantity: 13, // quantity updated
    user: { pin: 101283, id: 1, name: "John", bonusQty: 3 }, // pin copied
    address: { street: "First street", pin: 101283 },
  },
  {
    label: "Potato",
    id: 2,
    name: "Potato",
    quantity: 22,
    user: { id: 2, name: "Jane", bonusQty: 2 },
    address: { pin: 998472, street: "Second street" },
  },
];
*/

See also