obj-clean
Remove empty objects, empty strings, null and undefined values from objects
Last updated a year ago by samverschueren .
MIT · Repository · Bugs · Original npm · Tarball · package.json
$ cnpm install obj-clean 
SYNC missed versions from official npm registry.

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

Current Tags

  • 3.0.1                                ...           latest (a year ago)

7 Versions

  • 3.0.1                                ...           a year ago
  • 3.0.0                                ...           a year ago
  • 2.0.0                                ...           4 years ago
  • 1.1.1                                ...           4 years ago
  • 1.1.0                                ...           4 years ago
  • 1.0.1                                ...           5 years ago
  • 1.0.0                                ...           5 years ago
Maintainers (1)
Downloads
Today 0
This Week 0
This Month 6
Last Day 0
Last Week 2
Last Month 5
Dependencies (2)
Dev Dependencies (3)

Copyright 2014 - 2016 © taobao.org |