1、obj 检测对象
2、list 对象中不需要检测的属性

trim(str) {
    let str1 = (str === 0 || str) ? str.toString() : '';
    return str1 ? str1.replace(/(^\s*)|(\s*$)/g, "") : str;
}
isNullByObject(obj, list) {
        if (!Tools.isArray(list)) {
        return;
    }
    let flag = false;
    Object.keys(obj).forEach(item => {
        if (list.length) {
            if (list.indexOf(item) == -1 && !trim(obj[item])) {
                flag = true;
            }
        } else {
            if (!trim(obj[item])) {
                flag = true;
            }
        }
        
    });
    return flag;
}

对象示例:

let obj = { name: '平顶山', age: 20, sex: 'male', habby: ''};
isNullByObject(obj); // true habby值为空
isNullByObject(obj, ['habby']) // false habby跳过检验步骤

数组示例

let arr = [
    {name: '11', value: '11', type: ''},
    {name: '22', value: '22', type: ''},
    {name: '33', value: '33', type: ''}
];
let aa = false;
arr.forEach(element => {
    if (isNullByObject(element, ['type'])) {
        aa = isNullByObject(element, ['type']);
    }
});
console.log('数组对象校验==', aa);
posted on 2021-11-23 14:12  羽丫头不乖  阅读(1378)  评论(0编辑  收藏  举报