繁体   English   中英

如何同时检查 Object 和其他字段中的数组是否为空?

[英]How to check If array is empty in Object and the other fields at the same time?

假设我的 object 看起来像这样:

filter: {
masterName: '',
service:[],
}

如何检查数组和 masterName 字段是否为空?

只需评估它们每个的length属性,看看长度是否为0,如下例所示:

 var filter = { masterName: '', service: [], } // Checks whether masterName is zero-length if (filter.masterName.length === 0) { console.log('masterName is empty'); } // Checks whether service is zero-length if (filter.service.length === 0) { console.log('service is empty'); } // Checks whether either are zero-length if (filter.masterName.length === 0 && filter.service.length === 0) { console.log('Both are empty'); }

字符串长度在这里被描述为:

字符串 object 的长度属性包含字符串的长度,以 UTF-16 代码单元表示

数组长度在这里被描述为:

作为 Array 类型的实例的 object 的 length 属性设置或返回该数组中的元素数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM