Created
June 29, 2018 09:41
-
-
Save varmais/0582e2e67f54839baa24b17dfeadc3c1 to your computer and use it in GitHub Desktop.
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
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