Rename js snippets
This commit is contained in:
29
snippets/js/s/rename-object-keys.md
Normal file
29
snippets/js/s/rename-object-keys.md
Normal file
@ -0,0 +1,29 @@
|
||||
---
|
||||
title: Rename object keys
|
||||
type: snippet
|
||||
language: javascript
|
||||
tags: [object]
|
||||
cover: symmetry-cloudy-mountain
|
||||
dateModified: 2020-10-22T20:24:30+03:00
|
||||
---
|
||||
|
||||
Replaces the names of multiple object keys with the values provided.
|
||||
|
||||
- Use `Object.keys()` in combination with `Array.prototype.reduce()` and the spread operator (`...`) to get the object's keys and rename them according to `keysMap`.
|
||||
|
||||
```js
|
||||
const renameKeys = (keysMap, obj) =>
|
||||
Object.keys(obj).reduce(
|
||||
(acc, key) => ({
|
||||
...acc,
|
||||
...{ [keysMap[key] || key]: obj[key] }
|
||||
}),
|
||||
{}
|
||||
);
|
||||
```
|
||||
|
||||
```js
|
||||
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
|
||||
renameKeys({ name: 'firstName', job: 'passion' }, obj);
|
||||
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }
|
||||
```
|
||||
Reference in New Issue
Block a user