Files
30-seconds-of-code/snippets/lowerize.md
Angelos Chalaris a4874b6b68 Add 7 new snippets
2023-01-01 23:16:49 +02:00

598 B

title, tags, author, cover, firstSeen
title tags author cover firstSeen
Lowercase object keys object chalarangelo blog_images/building-facade.jpg 2023-02-12T05:00:00-04:00

Converts all the keys of an object to lower 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.toLowerCase() to lowercase the keys.
const lowerize = obj =>
  Object.keys(obj).reduce((acc, k) => {
    acc[k.toLowerCase()] = obj[k];
    return acc;
  }, {});
lowerize({ Name: 'John', Age: 22 }); // { name: 'John', age: 22 }