const isObject = val => typeof val === 'object' && !Array.isArray(val) && val !== null;
const isEmpty = val => (Array.isArray(val) && !val.length)
|| (isObject(val) && !Object.keys(val).length)
|| (isObject(val) && Object.keys(val).length && !Object.values(val).filter(key => key).length);
const err = {
1: {
2: { // section
151: 'Error', // form item error
160: 1,
161: ''
},
3: {}
},
2: {} // page
};
function removeEmptyKeys(obj) {
const keys = Object.keys(obj);
const newObj = {};
if (!keys.length) {
return obj;
}
keys.forEach(key => {
let value = obj[key];
if (value && !isObject(value)) {
newObj[key] = value;
} else if (isObject(value) && !isEmpty(value)) {
newObj[key] = removeEmptyKeys(value);
}
});
return newObj;
}
// const err = {
// }
console.log(removeEmptyKeys(err));
const pageId = 1,
sectionId = 2,
itemId = 151;
console.log(err[pageId][sectionId][itemId]);
let val = err[2][3];
const hasErrors = val ? isEmpty(val) : false;
console.log(hasErrors);