簡體   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