Files
30-seconds-of-code/snippets/js/s/upperize.md
2023-05-07 16:07:29 +03:00

617 B

title, type, language, tags, author, cover, dateModified
title type language tags author cover dateModified
Uppercase object keys snippet javascript
object
chalarangelo sofia-tram 2023-02-11T05:00:00-04:00

Converts all the keys of an object to upper case.

  • Use Object.keys() to get an array of the object's keys.
  • Use Array.prototype.reduce() to map the array to an object, using String.prototype.toUpperCase() to uppercase the keys.
const upperize = obj =>
  Object.keys(obj).reduce((acc, k) => {
    acc[k.toUpperCase()] = obj[k];
    return acc;
  }, {});
upperize({ Name: 'John', Age: 22 }); // { NAME: 'John', AGE: 22 }