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
});
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…