remove-empty

2.0.2 • Public • Published

remove-empty

Remove empty properties of an object. Property values that are empty, null, undefined, empty arrays and objects. It also cleans every nested object and its objects.

In addition, you can modify (remove and customize) your object property.


Installation

$ npm install remove-empty
// or
$ yarn add remove-empty

Usage and Examples

const removeEmpty = require('remove-empty');

const obj = {
    food: 'pizza',
    addOn: [],
    selection: {
        opt1: null,
        opt2: undefined,
        opt3: {
            food: 'burger',
            drink: 'coke',
            price: 20,
            dessert: {
                c1: 'halo-halo',
                c2: '',
                c3: {},
                dessert: 'mango float'
            }
        },
        opt4: {
            drink: 'sprite',
            price: 5,
        }
    }
};

Default

Here is the typical way of using remove-empty package.

removeEmpty(obj);

console.log(obj);
/* {
     food: 'pizza',
     selection: {
       opt3: {
         food: 'burger',
         drink: 'coke',
         price: 20,
         dessert: { 
                c1: 'halo-halo', 
                dessert: 'mango float' 
         }
       },
       opt4: { drink: 'sprite', price: 5 }
     }
   }

*/

Modify Object: remove / customize

To modify your object, you need to declare an Array with the name of property/ies of your choice.

In modifying your object you need to put at least three parameters in your removeEmpty variable:

  • First is the Object.
  • Second is the Array of your property names.
  • Third is the action type

action type: 'removeProperties'

Clean and remove every property of the object that is in the Array. Unless the property itself is an object, it will not be removed and will continue checking for similar properties.

const property = ['food', 'dessert'];

Now, we will use the provided object (obj) and property above as an example.

removeEmpty(obj, property, 'removeProperties');

// console.dir shows full object instead of '[Object]'
console.dir(obj, {depth: null});
/* {
     selection: {
       opt3: { 
            drink: 'coke',
            price: 20,
            dessert: {
                c1: 'halo-halo'
            }
       },
       opt4: { drink: 'sprite', price: 5 }
     }
   }
*/

action type: 'customProperties'

Clean and show every property of the object that is in the Array. If the property is not in the array but is an object, it will continue checking for similar properties.

removeEmpty(obj, property, 'customProperties');

console.dir(obj, {depth: null});
/* {
     food: 'pizza',
     selection: { 
            opt3: {
                food: 'burger',
                dessert: {
                    dessert: 'mango float' 
                }
            }
     }
   }

*/

People

MA Solano

Package Sidebar

Install

npm i remove-empty

Weekly Downloads

1

Version

2.0.2

License

ISC

Unpacked Size

7.12 kB

Total Files

8

Last publish

Collaborators

  • agnesorso