Modify the names of the own enumerable properties (keys) of an object.
Bug? Feature request? Please create an issue.
npm:
npm i rename-keys --save
bower install rename-keys --save
var rename = require('rename-keys');
rename( object, function );
Arguments
object {Object}
: the object to iterate over.function {Function}
: the function to use to rename each own enumerable property ofobject
.
The object to iterate over:
var pkg = require('./package.json');
and this renaming function:
var addDashes = function(str) {
return '--' + str;
};
console.log(rename(pkg, addDashes));
results in:
{ "--name": "rename-keys",
"--description": "Modify the names of the own enumerable properties (keys) of an object.",
"--version": "0.1.0",
"--homepage": "https://github.com/jonschlinkert/rename-keys",
"--author": {
"name": "Jon Schlinkert",
"url": "https://github.com/jonschlinkert"
} ... }
rename function now optionally receives the third boolean parameter. If true, the function renames nested objects' keys.
var addDashes = function(str) {
return '--' + str;
};
console.log(rename(pkg, addDashes, true));
results in:
{ "--name": "rename-keys",
"--description": "Modify the names of the own enumerable properties (keys) of an object.",
"--version": "0.1.0",
"--homepage": "https://github.com/jonschlinkert/rename-keys",
"--author": {
"--name": "Jon Schlinkert",
"--url": "https://github.com/jonschlinkert"
} ... }
Jon Schlinkert
Copyright (c) 2014-2015, Jon Schlinkert. Released under the MIT license