629 B
629 B
title, type, language, tags, author, cover, dateModified
| title | type | language | tags | author | cover | dateModified | |
|---|---|---|---|---|---|---|---|
| Symbolize object keys | snippet | javascript |
|
chalarangelo | computer-screens | 2021-08-01T05:00:00-04:00 |
Creates a new object, converting each key to a Symbol.
- Use
Object.keys()to get the keys ofobj. - Use
Array.prototype.reduce()andSymbolto create a new object where each key is converted to aSymbol.
const symbolizeKeys = obj =>
Object.keys(obj).reduce(
(acc, key) => ({ ...acc, [Symbol(key)]: obj[key] }),
{}
);
symbolizeKeys({ id: 10, name: 'apple' });
// { [Symbol(id)]: 10, [Symbol(name)]: 'apple' }