Process object properties deeply.
Install
$ npm i each-props --save
Usage
-
Load this module :
const eachProps = require('each-props');
-
Apply a function to all (non plain object) properties.
var obj = { a: 1, b: { c: 'CCC', d: { e: 'EEE' } } };
eachProps(obj, function(value, keyChain, nodeInfo) {
if (keyChain === 'a') {
nodeInfo.parent['a'] = value * 2;
} else if (keyChain === 'b.c') {
nodeInfo.parent['c'] = value.toLowerCase();
} else if (keyChain === 'b.d') {
return true;
} else if (keyChain === 'b.d.e') {
nodeInfo.parent['e'] = value.toLowerCase();
}
});
console.log(obj);
API
eachProps(obj, callback [, opts]) => void
Executes the callback function for all properties.
Arguments :
- obj [object] : A plain object to be treated.
- fn [function] : A function to treat the plain object.
- opts [object] : An object to be able to has options.
API of fn function
Properties of opts :
- opts [object]
- sort [function] : A sort function which orders the same level properties. (optional)
- ... and any properties you want to pass to each node.
License
Copyright (C) 2016 Takayuki Sato
This program is free software under MIT License.
See the file LICENSE in this distribution for more details.