簡體   English   中英

Javascript: object 數組,檢查item是否有值

[英]Javascript: object array, check if item has value

鑒於我有以下腳本,我如何才能只計算數組項是否定義了值,在以下情況下我得到 3,因為它計算了 arrays,但是如何檢查 firstName 或 lastname 的實際數組屬性是否為不為空,我希望它計算是否填充了名字或姓氏。

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
for (a in x) {
 noGuests++
}    
alert(noGuests)

更新我的應用程序的 javascript 渲染引擎已過時,無法使用過濾器。

蜘蛛猴 1.8.5

您可以按item.firstNameitem.lastName不為空的項目過濾數組並獲取結果數組的length

 var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}] const result = x.filter(item => item.firstName && item.lastName).length; console.log(result);

如果 firstName 或 lastName 中的任何一個被填充,那么這將被計算在內。 如果您同時需要 firstName 和 lastName,則將條件從||更改為&&

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
for (a in x) {
 if (x[a]['firstName'] || x[a]['lastName']) {
    noGuests++
 }
}

console.log(noGuests);

或者可以使用 forEach 循環

var x = [{"firstName":"guest1fnamee","lastName":"guest1fnamee"},{"firstName":"guest2fnamee","lastName":"guest2lnamee"},{"firstName":"","lastName":""}]

var noGuests = 0
x.forEach(x => {
  if (x.firstName || x.lastName) noGuests++;
})

console.log(noGuests);
function count_non_empty(collection) {
  return filter_non_empty(collection).length;
}

function filter_non_empty(collection) {
  return collection
    && collection.length
    && collection.filter(elem =>
      (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string
      || (elem.lastName || '').trim()  // <--- If lastName is empty or empty-like string
    ) || [];
}

插圖

 function count_non_empty(collection) { return filter_non_empty(collection).length; } function filter_non_empty(collection) { return collection && collection.length && collection.filter(elem => (elem.firstName || '').trim() // <--- If firstName is empty or empty-like string || (elem.lastName || '').trim() // <--- If lastName is empty or empty-like string ) || []; } let onlyLastName = { "firstName": "", "lastName": "SomeLastName" }; let onlyFirstName = { "firstName": "SomeFirstName", "lastName": "" }; let emptyLookingFirstName = { "firstName": " ", "lastName": "SomeLastName" }; let emptyLookingLastName = { "firstName": " SomeFIrstName ", "lastName": " " }; let havingBoth = { "firstName": "guest1fnamee", "lastName": "guest1fnamee" }; let notHavingAny = { "firstName": " ", "lastName": "" } let x = [{ "firstName": "guest1fnamee", "lastName": "guest1fnamee" }, { "firstName": "guest2fnamee", "lastName": "guest2lnamee" }, { "firstName": " ", "lastName": "" } ]; console.log("Count - x:", count_non_empty(x)); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth])); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingFirstName])); console.log("Count:", count_non_empty([onlyLastName, onlyFirstName, havingBoth, emptyLookingLastName])); console.log("Count - Not Having Any:", count_non_empty([notHavingAny])); console.log("Count - Having Both:", count_non_empty([havingBoth])); console.log("Count - Not Having Any with Having Both:", count_non_empty([notHavingAny, havingBoth])); console.log("Count - Not Having Any with Empty Looking First Name:", count_non_empty([notHavingAny, emptyLookingFirstName])); console.log("Count - Not Having Any with Empty Looking Last Name:", count_non_empty([notHavingAny, emptyLookingLastName]));


WYSIWYG => WHAT YOU SHOW IS WHAT YOU GET

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM