Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove empty objects from an object

I am trying to remove empty objects inside an object, here is an example with the expected output:

var object = {
    a: {
        b: 1,
        c: {
            a: 1,
            d: {},
            e: {
              f: {} 
            }
        }
    },
    b: {}
}


var expectedResult = {
    a: {
        b: 1,
        c: {
            a: 1,
        }
    }
}

I tried using some examples from other StackOverflow questions, however those are just for one level objects.

like image 784
Georgi Kirilov Avatar asked Mar 11 '17 13:03

Georgi Kirilov


2 Answers

Basic function that removes empty objects

First start with a function that only works with a single level of nesting.

This function removes all properties that reference an empty object:

function clearEmpties(o) {
  for (var k in o) {
    if (!o[k] || typeof o[k] !== "object") {
      continue // If null or not an object, skip to the next iteration
    }

    // The property is an object
    if (Object.keys(o[k]).length === 0) {
      delete o[k]; // The object had no properties, so delete that property
    }
    return o;
  }
}

Handling nested objects using recursion

Now you want to make it recursive so that it will operate on nested objects. So we already have tested if o[k] is an object, and we've tested if there are properties, so if there are, we simply call the function again with that nested object.

function clearEmpties(o) {
  for (var k in o) {
    if (!o[k] || typeof o[k] !== "object") {
      continue // If null or not an object, skip to the next iteration
    }

    // The property is an object
    clearEmpties(o[k]); // <-- Make a recursive call on the nested object
    if (Object.keys(o[k]).length === 0) {
      delete o[k]; // The object had no properties, so delete that property
    }
  }
    return o;
}

So just as the original call to clearEmpties removes properties of the given object that reference an empty object, likewise the recursive call will do the same for the nested objects.


Live demo:

var object = {
  a: {
    b: 1,
    c: {
      a: 1,
      d: {},
      e: { // will need to be removed after f has been removed
         f: {} 
      }
    }
  },

  b: {}
};

clearEmpties(object);
console.log(object);

function clearEmpties(o) {
  for (var k in o) {
    if (!o[k] || typeof o[k] !== "object") {
      continue
    }

    clearEmpties(o[k]);
    if (Object.keys(o[k]).length === 0) {
      delete o[k];
    }
  }
  return o;
}

Short version using Underscore and functional style

function clearEmpties(o) {
  if (_.isFunction(o) || !_.isObject(o)) return o;
  return _.chain(o)
    .mapObject(clearEmpties)
    .pick(p => !(_.isObject(p) && _.isEmpty(p)))
    .value();
}

Short version using lodash and functional style - works with treeshaking

import { isFunction, isObject, isEmpty, isArray, isPlainObject, fromPairs } from "lodash-es";

const removeEmtpyObjects = (o) => {
    if (isFunction(o) || !isPlainObject(o)) return o;

    if (isArray(o)) return o.map(removeEmtpyObjects);

    return fromPairs(
        Object.entries(o)
            .map(([k, v]) => [k, removeEmtpyObjects(v)])
            .filter(([k, v]) => !(v == null || (isObject(v) && isEmpty(v))))
    );
};
like image 154
8 revs, 5 users 74%user1106925 Avatar answered Nov 10 '22 21:11

8 revs, 5 users 74%user1106925


I had this same problem and with the addition that my object may contain arrays with empty elements that need to be removed as well.

I ended up with this quick and dirty solution.

If you want to define what "empty" means to you, I also added a different function. In my case, I also needed empty strings.

  function isEmpty(obj) {
        if (obj === '' || obj === null || JSON.stringify(obj) === '{}' || JSON.stringify(obj) === '[]' || (obj) === undefined || (obj) === {}) {
            return true
        } else {
            return false
        }
    }
    function removeEmpty(o) {
        if (typeof o !== "object") {
            return o;
        }
        let oKeys = Object.keys(o)
        for (let j = 0; j < oKeys.length; j++) {
            let p = oKeys[j]
            switch (typeof (o[p])) {
                case 'object':
                    if (Array.isArray(o[p])) {
                        for (let i = 0; i < o[p].length; i++) {
                            o[p][i] = removeEmpty(o[p][i])
                            if (isEmpty(o[p][i])) {
                                o[p].splice(i, 1)
                                i--
                            }
                        }
                        if (o[p].length === 0) {
                            if (Array.isArray(o)) {
                                o.splice(parseInt(p), 1)
                                j--
                            } else {
                                delete o[p]
                            }
                        }
                    } else {
                        if (isEmpty(o[p])) {
                            delete o[p]
                        } else {
                            o[p] = removeEmpty(o[p])
                            if (isEmpty(o[p])) {
                                delete o[p]
                            }
                        }
                    }
                    break
                default:
                    if (isEmpty(o[p])) {
                        delete o[p]
                    }
                    break
            }
    
        }
        if (Object.keys(o).length === 0) {
            return
        }
        return o
    }

Input:

var a = {
b: 1,
c: {
    d: [1, [[], [], [[[1], []]]], [2, [[], [[]]]], [], [[]]]
},
e: {
    f: [{}, { g: 1 }]
},
h: {
    i: { j: { k: undefined, l: null, m: { n: "", o: 1 } } }
},
p: { q: { r: 1 } }
}
removeEmpty(a)

Output:

   {
    "b": 1,
    "c": {
        "d": [1, [[[[1]]]], [2]]
    },
    "e": {
        "f": [{"g": 1}]
    },
    "h": {
        "i": {
            "j": {
                "m": {
                    "o": 1
                }
            }
        }
    },
    "p": {
        "q": {
            "r": 1
        }
    }
}
like image 1
Rubén Vega Avatar answered Nov 10 '22 23:11

Rubén Vega