742 B
742 B
title, type, tags, cover, dateModified
| title | type | tags | cover | dateModified | |
|---|---|---|---|---|---|
| Rename object keys | snippet |
|
fallen-leaves | 2020-10-22T20:24:30+03:00 |
Replaces the names of multiple object keys with the values provided.
- Use
Object.keys()in combination withArray.prototype.reduce()and the spread operator (...) to get the object's keys and rename them according tokeysMap.
const renameKeys = (keysMap, obj) =>
Object.keys(obj).reduce(
(acc, key) => ({
...acc,
...{ [keysMap[key] || key]: obj[key] }
}),
{}
);
const obj = { name: 'Bobo', job: 'Front-End Master', shoeSize: 100 };
renameKeys({ name: 'firstName', job: 'passion' }, obj);
// { firstName: 'Bobo', passion: 'Front-End Master', shoeSize: 100 }