鑒于我有以下腳本,我如何才能只計算陣列項是否定義了值,在以下場景中我得到 3,因為它計算陣列,但我如何檢查 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
uj5u.com熱心網友回復:
您可以按專案過濾陣列item.firstName并且item.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);
uj5u.com熱心網友回復:
如果填充了 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);
uj5u.com熱心網友回復:
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
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/431561.html
標籤:javascript 数组
