Created
April 22, 2024 15:06
-
-
Save moitorrijos/7c95775d5d8b686f3cda3a32f80ff3fb to your computer and use it in GitHub Desktop.
A function that renames all keys in an object
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const persons = [ | |
{ | |
name: 'Juan Perez', | |
nationality: 'Panamanian', | |
country_of_birth: 'Panama', | |
id_passport: '1', | |
date_of_birth: '2000-04-10', | |
}, | |
{ | |
name: 'Maria Perez', | |
nationality: 'Panamanian', | |
country_of_birth: 'Panama', | |
id_passport: '2', | |
date_of_birth: '2000-04-10', | |
}, | |
{ | |
name: 'Ana Perez', | |
nationality: 'Panamanian', | |
country_of_birth: 'Panama', | |
id_passport: '3', | |
date_of_birth: '2000-04-10', | |
}, | |
] | |
function renameKeysToTitleCase(arrObj: {}[]) { | |
function renameKey(obj: {}, old_key: string, new_key: string) { | |
if (old_key === new_key) return | |
if (!old_key.includes('_')) return | |
const descriptor = Object.getOwnPropertyDescriptor(obj, old_key) | |
if (!descriptor) return | |
Object.defineProperty(obj, new_key, descriptor) | |
delete obj[old_key] | |
} | |
const toTitleCase = (str: string) => { | |
const strArray = str.toLowerCase().split('_') | |
const strArrayShift = strArray.shift() | |
const otherStringsArray = strArray | |
.map(function (word) { | |
return word.charAt(0).toUpperCase() + word.slice(1) | |
}) | |
.join('') | |
return strArrayShift + otherStringsArray | |
} | |
arrObj.forEach((obj) => { | |
Object.keys(obj).forEach((key) => { | |
renameKey(obj, key, toTitleCase(key)) | |
}) | |
}) | |
return arrObj | |
} | |
// Test it for yourself: | |
console.log(renameKeysToTitleCase(persons)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment