Search code examples
javascript

Remove empty string field from a object contain nested object and array?


I have ask another question, but someone close that question. I really need this answer. That's why I asking another question.

I have a object like following. I have to remove that empty string filed from nested object and also from nested array. How can I remove that.

const obj = {
  name: 'Red Boy',
  price: '350',
  originalPrice: '', // Empty string field
  stock: 20,
  category: {
    name: '', // Empty String field
    subCategory: { name: ''} // Empty String filed 
  },
  weight: '90kg',
  dimensions: {
    width: '50cm',
    height: '', // Empty string filed
    length: '70cm'
  },
  suitable: [
     { name: 'Yoga' },
     { name: '' }, // Empty String filed
     { name: 'Winter' }
  ],
  additionalInfo: [
     { field: 'closure', value: 'Button' },
     { field: 'collar', value: ''} // Empty String Field 
  ]
}

In this hybrid object type you can see some sub-object and also some sub-array. You can also see some field that are not contain any value.(I comment out that filed).

Actually I need to remove that filed. How can I remove that empty string field from above hybrid object type.

Thank you..

My Expected result-

{
  name: 'Red Boy',
  price: '350',
  // Removed
  stock: 20,
  category: {
    name: '', // Empty String field
    // Removed
  },
  weight: '90kg',
  dimensions: {
    width: '50cm',
    // Removed
    length: '70cm'
  },
  suitable: [
     { name: 'Yoga' },
     //Removed
     { name: 'Winter' }
  ],
  additionalInfo: [
     { field: 'closure', value: 'Button' },
     { field: 'collar', //Removed }
     // Here If this two filed is empty then should remove the whole object
     { field: '', value: '' }
     // Then should remove whole '{ field: '', value: '' }'
  ]
}

Solution

  • const obj = {
      name: 'Red Boy',
      price: '350',
      originalPrice: '', // Empty string field
      stock: 20,
      category: {
        name: '', // Empty String field
        subCategory: { name: ''} // Empty String filed 
      },
      weight: '90kg',
      dimensions: {
        width: '50cm',
        height: '', // Empty string filed
        length: '70cm'
      },
      suitable: [
         { name: 'Yoga' },
         { name: '' }, // Empty String filed
         { name: 'Winter' }
      ],
      additionalInfo: [
         { field: 'closure', value: 'Button' },
         { field: 'collar', value: ''} // Empty String Field 
      ]
    }
    
    function removeEmptyString(object) {
        Object
            .entries(object)
            .forEach(([key, value]) => {
                if (value && typeof value === 'object')
                    removeEmptyString(value);
                if (value && 
                    typeof value === 'object' && 
                    !Object.keys(value).length || 
                    value === null || 
                    value === undefined ||
                    value.length === 0
                ) {
                    if (Array.isArray(object))
                        object.splice(key, 1);
                    else
                        delete object[key];
                }
            });
        return object;
    }
    
    console.log(removeEmptyString(obj))