rundef
v1.2.6
Published
Remove undefined properties from object
Downloads
2,378
Readme
rundef
Remove undefined
properties from object.
N.B. Does not remove null
or falsy values, just undefined
.
Install
$ npm install rundef
$ yarn add rundef
Usage
const rundef = require('rundef');
import rundef from 'rundef';
For the most accurate examples, see the test.js
file
Basic
const input = {
a: undefined,
b: 1
}
rundef(input); // { b: 1 }
Advanced
rundef
supports two options:
mutate
boolean - if truthy, the original object will be mutated; if falsy, a new object will be constructed and returned. Defaults tofalse
recursive
boolean | int - whetherrundef
should recursively process nested objects. If it's an integer, it will specify the number of nested layers, or levels, to process. If it is set totrue
, it will recursively process all layers. Defaults to0
, which is equivalent tofalse
.
const input = {
a: undefined, // Level 0
b: {
c: 1,
d: undefined, // Level 1
e: {
f: undefined // Level 2
}
}
}
const output = rundef(
input,
false, // mutate - whether to mutate the original object or return a new one
1, // recursive - whether to apply recursively
);
output;
{ // Level 0
b: {
c: 1, // Level 1
e: {
f: undefined // Level 2 - Not removed as level 1 was specified
}
}
}