Modify the names of the key properties (keys) of an object util a defined level of depth.
Bugs? Please create an issue. Be aware that this is one of my first Javascript, so it is keen to be better coded ;)
var rename = require('rename-keys-multidepth');
rename(object, function, level);
Arguments
object {Object}
: the object to iterate over.function {Function}
: the function to use to rename each own enumerable property ofobject
.level {Integer}
: the maximum level the renaming is done. By default is set to rename in the whole object. Level count starts by 0.
var toBeChanged = [
{
"name": "Foo",
"amount": 55
},
{
"name": {
"name": 23
},
"amount": 33
},
{
'name': ['id', 'name']
},
{
'name': ['level', {'name': 'bar'}]
}
];
var replaceName = function (key) {
return (key === 'name') ? 'changed' : key;
}
console.log(replaceName(toBeChanged, replaceName));
Jorge Lanza
Copyright (c) 2016, Jorge Lanza. Released under the MIT license
Thanks to Jon Schlinkert for providing the basics to start this small project.
Thanks to Shamasis Bhattacharya from one of his his blog posts I took the isArray()
function.