Files
30-seconds-of-code/snippets/lowercaseKeys.md
Isabelle Viktoria Maciohsek c3a2e47672 Add prototype to descriptions
2020-10-20 11:21:07 +03:00

627 B

title, tags
title tags
lowercaseKeys object,intermediate

Creates a new object from the specified object, where all the keys are in lowercase.

  • Use Object.keys() and Array.prototype.reduce() to create a new object from the specified object.
  • Convert each key in the original object to lowercase, using String.prototype.toLowerCase().
const lowercaseKeys = obj =>
  Object.keys(obj).reduce((acc, key) => {
    acc[key.toLowerCase()] = obj[key];
    return acc;
  }, {});
const myObj = { Name: 'Adam', sUrnAME: 'Smith' };
const myObjLower = lowercaseKeys(myObj); // {name: 'Adam', surname: 'Smith'};