5
var template = {
    personal: {},
    education: {},
    certificate: [{"test": "Test"}, {}, {}],
    experience: []
}

removeEmptyObj(template);

function removeEmptyObj(obj)
for (var key in obj) {
    console.log("Foor Loop" + key + " " + obj[key]);
    if (_.isObject(obj[key]) && !_.isEmpty(obj[key])) {
        console.log("Second Loop Object:::" + key + " " + obj[key]);
        removeEmptyObj(obj[key]);
    }
    if (_.isEmpty(obj[key])) {
        console.log("Delete Object:::" + key + " " + obj[key]);
        obj = _.omitBy(obj, _.isEmpty);
    }
}
console.log(obj);
return obj;
}

Current Output is : {certificate: [{"test": "Test"}, {}, {}]}

Desired Output : {certificate: [{"test": "Test"}]}

What's wrong here your help appreciate :)

5 Answers 5

5

You can _.transform() the object recursively to a new one, and clean empty objects and arrays on the way.

Note: I've added more elements to the structure for demonstration

var template = {
    personal: {},
    education: {},
    certificate: [{}, {"test": "Test"}, {}, {}, [{}], [{}, 1, []]],
    experience: [[1, 2, [], { 3: [] }]]
};

function clean(el) {
  function internalClean(el) {
    return _.transform(el, function(result, value, key) {
      var isCollection = _.isObject(value);
      var cleaned = isCollection ? internalClean(value) : value;

      if (isCollection && _.isEmpty(cleaned)) {
        return;
      }

      _.isArray(result) ? result.push(cleaned) : (result[key] = cleaned);
    });
  }

  return _.isObject(el) ? internalClean(el) : el;
}

console.log(clean(template));
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.17.4/lodash.min.js"></script>

Sign up to request clarification or add additional context in comments.

3 Comments

This leaves gaps in arrays. For example [{}, { test: 1 }] becomes [undefined, { test: 1 }].
@trincot - thanks for the catch. I've refactored it, and now it will clean everything recursively.
Could still be improved to work correctly when passing a primitive as argument: e.g. clean("abc") currently returns an object with three properties, while it should just return "abc".
3

You could use this function, making the distinction between plain objects and arrays:

// Helper function
var isEmptyObject = _.overEvery(_.isObject, _.isEmpty);
// Actual function
function removeEmptyObj(obj) {
    return  _.isArray(obj)  ? _.reject(_.map(obj, removeEmptyObj), isEmptyObject)
          : _.isObject(obj) ? _.omitBy(_.mapValues(obj, removeEmptyObj), isEmptyObject)
          : obj; 
}

// Example
var template = {
    personal: {},
    education: {},
    certificate: [{"test": "Test"}, {}, {}],
    experience: []
}

var result = removeEmptyObj(template);

console.log(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/lodash.js/4.16.4/lodash.min.js"></script>

Comments

1

No need for Lodash for that. A simple filter will do.

var template = {
    personal: {},
    education: {},
    certificate: [{"test": "Test"}, {}, {}],
    experience: []
}

template.certificate = template.certificate.filter(o => Object.keys(o).length)

console.log(template.certificate)

5 Comments

I need recursively
I am not sure certificate is always empty like this maybe also another object is empty like this personal,education,certificate,experience etc.. need recursively
Ah, I followed your current output / desired output.
look at my O/P problem in just certificate not personal,education,experience it's already remove as per my requirement.
look at my O/P problem in just certificate just problem not in a personal,education,experience it's already remove as per my requirement.
0

Have you tried _.omit(certificate, [{}]). I have never tried but let me know

3 Comments

You mean in a deep way? Because this method will remove all occurrence existing at the same level
yaah deep way if you look at my O/P problem in just certificate not personal,education,experience it's already remove as per my requirement.
Ok trying to setup a quick project to be able to help you
0

You can try this method, it works for me with your example. I'm pretty sure we can do in a better lodash way but at least you re unblock

deepOmit(obj) {
    function omitFromObject(obj) {
        return _.transform(obj, function(result, value, key) {
            if (_.isNull(value) || _.isUndefined(value) || _.isEmpty(value)) {
                return;
            }

            result[key] = _.isObject(value) ? omitFromObject(value) : value;
        });
    }

    return omitFromObject(obj);
}

4 Comments

I just check it.it's doesn't work but thank for your answer @drioemgaoin
Have you tried on the same example you provided or something more complex? You may give us more test context because to be few people to provide an example working on our side with your example.
I use @OriDrori example look at him answer
Ok super and he gave you a final working solution! Nice job!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.