JavaScript

如何跳过school判断name和age和sex是否为空,如果为空返回false,否则true


   let obj={
        name:'',
        age:0,
        sex:'男',
        school:'南校'
    }
    function hasNull(obj) {//name,age或者sex其中有一个存在等价于false的值返回true
        return !obj.name || !obj.age || !obj.sex ? true : false;
    }

    console.log(hasNull({ name: '', age: 0, sex: '男', school: '南校' }));//true
    console.log(hasNull({ name: 'xx', age: 0, sex: '男', school: '南校' }));//true
    console.log(hasNull({ name: 'xx', age: 19, sex: '男', school: '南校' }));//false

其中一个为空就返回false return obj.name&&obj.age&&obj.sex;
全部为空才返回false return obj.name || obj.age||obj.sex;

  • 给你找了一篇非常好的博客,你可以看看是否有帮助,链接:JavaScript
您好,我是有问必答小助手,您的问题已经有小伙伴帮您解答,感谢您对有问必答的支持与关注!
PS:问答VIP年卡 【限时加赠:IT技术图书免费领】,了解详情>>> https://vip.csdn.net/askvip?utm_source=1146287632