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;
}
電流輸出爲刪除空對象:{certificate: [{"test": "Test"}, {}, {}]}
如何從JSON遞歸使用lodash
所需的輸出:{certificate: [{"test": "Test"}]}
什麼是錯在這裏你的幫助感激:)
這會在數組中留下空隙。例如'[{},{test:1}]'變成'[undefined,{test:1}]''。 – trincot
@trincot - 感謝您的收穫。我重構了它,現在它會遞歸地清理一切。 –
它的工作就像魅力:) @OriDrori –