Skip to content

Latest commit

 

History

History
45 lines (35 loc) · 1.19 KB

copyKeys.md

File metadata and controls

45 lines (35 loc) · 1.19 KB

copyKeys (source code)

  • Curried: true
  • Failsafe status: alternative available

The copyKeys function is similar to the renameKeys function but retains both the source and destination keys in the resulting array. For deep copying of nested objects refer to copyKeysDeep function.

Arguments:

  • keyMap: An object where the keys are the original keys of the array of objects and values are the new keys that will be added in the object.
    {
      sourceKey1: "destinationKey1",
      sourceKey2: "destinationKey2",
    }
  • objectArray: The array of objects on which the copy function works.

Usage:

const data = [
  { id: 1, name: "Tomato", quantity: 10 },
  { id: 2, name: "Potato", quantity: 20 },
];

// copy name to label and id to value
copyKeys({ name: "label", id: "value" }, data);

/*
output: [
  { label: "Tomato", value: 1, id: 1, name: "Tomato", quantity: 10 },
  { label: "Potato", value: 2, id: 2, name: "Potato", quantity: 20 },
];
*/

See also