Skip to content

Instantly share code, notes, and snippets.

@varmais
Created June 29, 2018 09:41
Show Gist options
  • Save varmais/0582e2e67f54839baa24b17dfeadc3c1 to your computer and use it in GitHub Desktop.
Save varmais/0582e2e67f54839baa24b17dfeadc3c1 to your computer and use it in GitHub Desktop.
Object.prototype.renameProperty = function (oldName, newName) {
if (this.hasOwnProperty(oldName)) {
this[newName] = this[oldName];
delete this[oldName];
}
return this;
};
const mapping = [
{old: 'something1', new: 'something_else'},
{old: 'something2', new: 'something_else'},
{old: 'alice', new: 'bob'}
];
const object1 = {
something1: 'first prop',
alice: 'second prop'
};
const object2 = {
something2: 'first prop',
alice: 'second prop'
};
function mapProperties (object) {
mapping.forEach((mapper) => {
object.renameProperty(mapper.old, mapper.new);
});
console.log(object);
}
(function () {
mapProperties(object1);
mapProperties(object2);
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment