obj-clean
TypeScript icon, indicating that this package has built-in type declarations

3.0.1 • Public • Published

obj-clean Build Status

Remove empty objects, empty arrays, empty strings, null and undefined values from objects.

Install

$ npm install --save obj-clean

Usage

const clean = require('obj-clean');
 
clean({foo: ''});
//=> {}
 
clean({foo: 'bar', baz: undefined});
//=> {foo: 'bar'}
 
clean({foo: {bar: 'baz', baz: null, bax: false}});
//=> {foo: {bar: 'baz', bax: false}}
 
clean({foo: {bar: 'baz', baz: {}});
//=> {foo: {bar: 'baz'}}
 
clean({foo: 'bar', baz: []});
//=> {foo: 'bar', baz: []}
 
clean({foo: [{unicorn: ''}]});
//=> {foo: []}
 
clean({foo: [{unicorn: '', rainbow: '🌈'}]});
//=> {foo: [{rainbow: '🌈'}]}
 
clean({foo: [{unicorn: ''}]}, {cleanArrays: false});
//=> {foo: [{unicorn: ''}]}
 
clean({foo: [{unicorn: ''}]}, {preserveArrays: false});
//=> {}

API

clean(obj, [options])

Returns a clean object.

obj

Type: Object

The object to clean up.

options

preserveArrays

Type: boolean
Default: true

Set to false if you want to remove empty arrays.

cleanArrays

Type: boolean
Default: true

Set to false if you don't want to iterate over arrays and clean all the objects inside the array.

License

MIT © Sam Verschueren

Package Sidebar

Install

npm i obj-clean

Weekly Downloads

2,903

Version

3.0.1

License

MIT

Unpacked Size

6.35 kB

Total Files

5

Last publish

Collaborators

  • samverschueren