错误原因:
每次array.length都会变化减小,但是i的没次都会增加,等到删到一半的时候,i的值已经增加到和length一样的长度了,导致不会在循环,前面的值就没有删掉了。
想要的效果是循环遍历数组把空的删掉
正确代码:
/**
* @author: shapec
* @description: 剔除对象数组中的空对象
* @param {*} filterEmptyObj
* @return {*}
*/
export const filterEmptyObj = (arr) => {
arr.forEach((i) => {
for (let j = i.children.length - 1; j >= 0; j--) {
if (Object.keys(i.children[j]).length === 0) {
i.children.splice(j, 1);
}
}
});
return arr;
};