Skip to content
Advertisement

How to remove empty object in array?

I am trying to remove the empty object {} from the below structure.

JavaScript

This is my JavaScript code where I process the data and trying to remove the empty object from the result.

JavaScript

I tried checking null and undefined and also with property check bool as false. Since the empty {} is always returning length as 1, that is also ruled out. I am stuck here on processing the removal of empty object.

Above code is removing the entire recs node. Can you help me find what I am missing?

Advertisement

Answer

Check the length of the Object.keys() to see if object is empty or not.

JavaScript

You can’t iterate all the dynamic levels of array manually, so better to write the function which has recursive function call.

JavaScript
User contributions licensed under: CC BY-SA
10 People found this is helpful
Advertisement