https://github.com/d4nyll/rundef
Remove undefined properties from object
https://github.com/d4nyll/rundef
javascript object undefined
Last synced: about 1 year ago
JSON representation
Remove undefined properties from object
- Host: GitHub
- URL: https://github.com/d4nyll/rundef
- Owner: d4nyll
- License: mit
- Created: 2017-12-26T19:00:02.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-08T06:51:54.000Z (almost 2 years ago)
- Last Synced: 2025-04-08T20:46:57.061Z (about 1 year ago)
- Topics: javascript, object, undefined
- Language: JavaScript
- Size: 92.8 KB
- Stars: 3
- Watchers: 1
- Forks: 1
- Open Issues: 5
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# rundef
Remove `undefined` properties from object.
[](https://travis-ci.org/d4nyll/rundef) [](https://codecov.io/gh/d4nyll/rundef) [](https://www.codacy.com/app/d4nyll/rundef?utm_source=github.com&utm_medium=referral&utm_content=d4nyll/rundef&utm_campaign=Badge_Grade) [](https://www.codefactor.io/repository/github/d4nyll/rundef) [](https://codeclimate.com/github/d4nyll/rundef/test_coverage) [](https://codeclimate.com/github/d4nyll/rundef/maintainability) [](https://nodesecurity.io/orgs/d4nyll/projects/d5c67ec9-8c1b-4aef-8971-fe60572adc08) [](https://snyk.io/test/github/d4nyll/rundef) [](https://greenkeeper.io/)
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fd4nyll%2Frundef?ref=badge_shield)
**N.B.** Does not remove `null` or falsy values, just `undefined`.
## Install
[](https://nodei.co/npm/rundef/)
```
$ 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 to `false`
* `recursive` _boolean | int_ - whether `rundef` 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 to `true`, it will recursively process all layers. Defaults to `0`, which is equivalent to `false`.
```
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
}
}
}
```
## License
[](https://app.fossa.io/projects/git%2Bgithub.com%2Fd4nyll%2Frundef?ref=badge_large)