我正在嘗試過濾大量資料,這些資料具有嵌套在 whos 值中的陣列,我需要與字串進行比較。為了比較它們,我需要清理字串,因為它來自用戶輸入和空格/大寫可能會有所不同。所以我讓我的函式通過一個看起來像這樣的過濾器
資料最初看起來像
formularyOptions = [{Condition: "headache"...}{Condition: "hair loss"..}...]
chiefComplaint = "Headache"
const cleanText = (value) => {
let str = value;
if (!str) {
return value;
}
str = str.toLowerCase();
str = str.replace(/\s/g, "");
return str;
};
let formularyList = formularyOptions.filter(
(item) => !!chiefComplaint && cleanText(item.Condition) === cleanText(chiefComplaint),
);
這作業得很好,但現在
我的資料如下所示:
[{Condition: ["headache", "migraine"]...}{Condition: ["hair loss"]..}...]
我試過改變我的過濾器來回圈遍歷條件陣列,但由于某種我不明白的原因,它沒有回傳任何東西。并且包含方法不起作用,因為它區分大小寫。關于如何解決這個問題,甚至為什么 forEach 不能在 .filter 中作業的任何建議都會非常有幫助,這是我對 for 回圈的嘗試:
let formularyList = formularyOptions.filter(
(item) => !!chiefComplaint && item.Condition.forEach((condition) => cleanText(condition) === cleanText(chiefComplaint)),
);
它只回傳一個空陣列..
uj5u.com熱心網友回復:
您.forEach(...)在布爾條件中包含 a ,但它是無效的,它只回圈,不回傳任何內容。
我認為您實際上需要使用.some(...)代替,它將嘗試找到與條件相對應的一些專案:
let formularyList = formularyOptions.filter(
(item) => !!chiefComplaint && item.Condition.some((condition) => cleanText(condition) === cleanText(chiefComplaint)),
);
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/373430.html
標籤:javascript 数组 排序 筛选 foreach
上一篇:Java從二維陣列回傳對稱對
