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.
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.
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 },
];
*/