renameKey
renameKey(obj, oldKey, newKey)
renameKey(obj, oldKey, newKey)
Renames a key in an object from an old name to a new name.
Description
The renameKey
function renames a key in the provided object from an old name to a new name. It ensures that the value associated with the old key is preserved and assigned to the new key. If the old key does not exist or the new key is the same as the old key, the function does not perform any action.
Arguments
obj
(object
): The object to rename the key in.oldKey
(string
): The old name of the key to rename.newKey
(string
): The new name of the key.
Returns
void
: This function does not return a value; it modifies the provided object in place.
Example
Usage
Notes
The
renameKey
function modifies the provided object in place, renaming the specified key.If the old key does not exist in the object or the new key is the same as the old key, the function does not perform any action.
This function is useful for scenarios where you need to change the name of a key in an object without affecting its value.
Last updated
Was this helpful?