Skip to content

Latest commit

 

History

History
60 lines (45 loc) · 1.57 KB

README.md

File metadata and controls

60 lines (45 loc) · 1.57 KB

rename-keys-multidepth NPM version

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 ;)

Quickstart

Usage

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 of object.
  • level {Integer}: the maximum level the renaming is done. By default is set to rename in the whole object. Level count starts by 0.

Example

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));

Authors

Jorge Lanza

Licence

Copyright (c) 2016, Jorge Lanza. Released under the MIT license

Acknowledgments

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.