87
rated 0 times
[
94]
[
7]
/ answers: 1 / hits: 29149
/ 8 Years ago, fri, july 8, 2016, 12:00:00
I have an object with several properties and I would like to remove objects/nested objects that are empty, using lodash. What is the best way to do this?
Let template = {
node: test,
representation: {
range: { }
},
transmit: {
timeMs: 0
}
};
to
template = {
node: test,
transmit: {
timeMs: 0
}
};
I tried something like this, but I am lost.
Utils.removeEmptyObjects = function(obj) {
return _.transform(obj, function(o, v, k) {
if (typeof v === 'object') {
o[k] = _.removeEmptyObjects(v);
} else if (!_.isEmpty(v)) {
o[k] = v;
}
});
};
_.mixin({
'removeEmptyObjects': Utils.removeEmptyObjects
});
More From » object