iter-object
v2.0.0
Published
An object iterator and object replacer with callbacks for key-value pairs
Downloads
2,278
Maintainers
Readme
iter-object
An object iterator and object replacer with callbacks for key-value pairs
Installation
npm install iter-object
or
yarn add iter-object
Usage
| Method | Params | Description | |--|--|--| | iter_obj | (src: Object, fn: Function ) | Iterate over objects and display key-value pairs | | iter_repl | (src: Object, fn: Function ) => any | Iterate over object and return modified or new values |
iterObject
Example:
import { iter_obj } from 'iter-object';
const src = {
owner: 'Alex',
car: {
make: 'Toyota',
model: 'Matrix',
year: 2013
}
}
iter_obj(src, (key, value) => {
console.log(key, value);
});
// output:
// owner Alex
// car { make: 'Toyota', model: 'Matrix', year: 2013 }
// make Toyota
// model Matrix
// year 2013
iterReplace
Example:
import { iter_repl } from 'iter-object';
const sellerContent = {
owner: 'Alex',
car: {
make: 'Toyota',
model: 'Matrix',
year: 2013,
},
};
const buyerContent = iter_repl(sellerContent, (key, value) => {
switch(key) {
case 'owner': {
return 'New Buyer';
}
case 'car': {
return { ...value, color: 'White' };
}
default: {
return value;
}
}
});
console.log(JSON.stringify(buyerContent, null, 2));
// output:
// {
// "owner": "New Buyer",
// "car": {
// "make": "Toyota",
// "model": "Matrix",
// "year": 2013,
// "color": "White"
// }