function removeEmptyObject(object){ for (var i in object) { var value = object[i]; if (typeof value === 'object') { if (Array.isArray(value)) { if (value.length == 0) { delete object[i]; continue; } } removeEmptyObject(value); //按需添加 if (isEmpty(value)) { delete object[i]; } } else { if (value === '' || value === null || value === undefined) { delete object[i]; } else { } } } return object;}//是否为空function isEmpty(object) { for (var name in object) { return false; } return true;}
操作组织架构JSON数据,移除空键值,子集内容合并到新键children下。
function removeEmptyObject(object){ for (var i in object) { var value = object[i]; if (typeof value === 'object') { if (Array.isArray(value)) { if (value.length == 0) { //alert(object[i]); delete object[i]; continue; }else{ var a=[]; if(i=='jobChildren'){ for (var j = 0; j < object[i].length; j++) { a.push(object[i][j]); } delete object[i]; }else if(i=='userChildren'){ for (var j = 0; j < object[i].length; j++) { a.push(object[i][j]); } delete object[i]; }else if(i=='deptChildren'){ for (var j = 0; j < object[i].length; j++) { a.push(object[i][j]); } delete object[i]; }else if(i=='companyChildren'){ for (var j = 0; j < object[i].length; j++) { a.push(object[i][j]); } delete object[i]; }else if(i=='jobDeptChildren'){ for (var j = 0; j < object[i].length; j++) { a.push(object[i][j]); } delete object[i]; } if(a.length>0&&object["children"]!=undefined){ for (var k = 0; k < object["children"].length; k++) { a.push(object["children"][k]); } object["children"]=a; }else if(a.length>0 && object["children"]==undefined){ object["children"]=a; } } } removeEmptyObject(value); if (isEmpty(value)) { delete object[i]; } } else { if (value === '' || value === null || value === undefined) { delete object[i]; } else { } } } } function isEmpty(object) { for (var name in object) { return false; } return true; }