-
Notifications
You must be signed in to change notification settings - Fork 0
Library: clean()
Eugene Lazutkin edited this page Jul 3, 2018
·
3 revisions
clean() module is a function, which recursively removes a property in place with the given name from an object on all levels.
clean() works faster than cleanRegExp() but less flexible. If you want to remove several differently named properties consider cleanRegExp().
In a module-enabled environment (like Babel) it can be accessed like that:
import clean from '@researchnow/reno/src/utils/clean';In global-based environments (like a browser) it is frequently mapped to Reno.utils.clean.
The function takes the following arguments:
-
objectis an object, which will be recursively traversed and checked for a property by name. If found, that property is going to be deleted. -
nameis a property name to look for. The default:'_id'.
It returns object.
Using the default:
const object = {_id: 1, value: 'x', next: {_id: 2, value: 'y', next: null}};
clean(object);
assert(isEqual(object,
{value: 'x', next: {value: 'y', next: null}));Custom removal:
const object = [
{_id: 1, _meta: {account: 42}, value: 'x'},
{_id: 2, _meta: {account: 66}, value: 'y'}
];
clean(object, '_meta');
assert(isEqual(object,
[
{_id: 1, value: 'x'},
{_id: 2, value: 'y'}
]
));