Skip to content

Modify the names of the key properties (keys) of an object util a defined level of depth.

License

Notifications You must be signed in to change notification settings

jlanza/rename-keys-multidepth

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Modify the names of the key properties (keys) of an object util a defined level of depth.

Resources

License

Stars

Watchers

Forks

Packages

No packages published