rename-keys
Modify the names of the own enumerable properties (keys) of an object.
Bug? Feature request? Please create an issue.
Quickstart
npm i rename-keys --save
bower install rename-keys --save
Usage
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 of object
.
Example
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 based on value
The value can also be passed in as as a second argument.
rename({a: 1, b: 2, c: 3}, function(key, value) {
if (value > 1) return key + 'x';
});
results in:
{a: 1, bx: 2, cx: 3});
Authors
Jon Schlinkert
License
Copyright (c) 2014-2015, Jon Schlinkert.
Released under the MIT license