美文网首页
2.1 JS中对象判空

2.1 JS中对象判空

作者: 不会忘的名字 | 来源:发表于2021-10-21 09:31 被阅读0次
    <script type="text/javascript">
        $(document).ready(function(){
            let _arrObj = {name: '我是程序猿', age: 18, sex:'男'};
            // 1.JSON字符串判空
            //console.log( JSON.stringify(_arrObj) === '{}' )
    
            // 2. => 函数
            let _fn = (_d) => {
                for (let _key in _d){
                    return false;
                }
                return true;
            };
            //console.log( _fn(_arrObj) )
    
            // 3.getOwnPropertyNames(),获取js对象所有属性名, 存到一个数组中
            let _n3 = Object.getOwnPropertyNames(_arrObj);
            //console.log( _n3.length > 0 )
    
            // 4.获取js对象所有可枚举属性,存到一个数组中
            let _n4 = Object.keys(_arrObj);
            console.log( _n4.length > 0 )
    
            // 5.hasOwnProperty()判断对象是否含有该属性名
            let _fn2 = (_d) => {
                for (let _key in _d){
                    if ( _d.hasOwnProperty(_key) ){
                        return false;
                    }
                }
                return true;
            };
            //console.log(_fn2(_arrObj))
        });
    </script>
    

    相关文章

      网友评论

          本文标题:2.1 JS中对象判空

          本文链接:https://www.haomeiwen.com/subject/hswtaltx.html