我有一個 for..of 回圈作為過濾器函式的回呼函式。我遇到的問題是我在回呼中測驗的物件僅迭代一次,因此只有第一個值應用于過濾器。有沒有辦法強制我的 for 回圈迭代次數作為物件的長度?我知道使用陣列 for 回圈這是可能的,但不確定物件。
這是一個例子:
const search = {
Condition: "Allergies",
ProductName: "Acetaminophen"
}
const meds =
[ { Active : false
, Condition : [ 'Allergies']
, DaysSupply : 0
, Doseform : 'Tablet'
, ProductName : 'Acetaminophen 650 mg tablet, extended release'
, Protocol : 'PATIENT COMMUNICATION: Price $10. 100 tablets'
, Quantity : 100
}
, { Active : false
, Condition : [ 'COVID-19 Symptoms']
, DaysSupply : 0
, Doseform : 'Tablet'
, ProductName : 'Acetaminophen 650 mg tablet, extended release'
, Protocol : 'PATIENT COMMUNICATION: Price is $10. 100 tablets'
, Quantity : 100
}
, { Active : false
, Condition : [ 'Headaches (Migraine & Tension)']
, DaysSupply : 0
, Doseform : 'Tablet'
, ProductName : 'Acetaminophen 650 mg tablet, extended release'
, Protocol : 'PATIENT COMMUNICATION: Price is $10. 100 tablets'
, Quantity : 100
}
, { Active : false
, Condition : [ 'Allergies']
, DaysSupply : 0
, Doseform : 'Capsule'
, ProductName : 'Amoxicillin 500 mg capsule'
, Protocol : 'PATIENT COMMUNICATION: Price is $20- 21 capsules'
, Quantity : 21
}
]
const comparedtosearch = (medication, searchParams) => {
for (let [key, value] of Object.entries(searchParams)) {
// do(es) the object(s) contain the search value
if (key === "Condition") {
const includesSearch = (item) => item.toLowerCase().includes(value.toLowerCase())
// conditions are arrays
return medication[key].some(includesSearch)
} else if (key != "Condition") {
return medication[key].toLowerCase().includes(value.toLowerCase())
}
}
}
const handleSearch = (searchParams) => {
// clean copy of the medications (formularies)
const medications = [...meds]
const formFiltered = medications.filter((med) => comparedtosearch(med, searchParams))
console.log(formFiltered)
//setLoading(false)
}
handleSearch(search)
.as-console-wrapper {max-height: 100% !important;top: 0;}
.as-console-row::after {display: none !important;}
我的預期結果將是與搜索中傳遞的所有值具有匹配值的專案。物件中的所有值meds
都可以添加到search
物件中,因為該物件是從用戶輸入欄位構建的,并在用戶編輯欄位時傳入。我知道我可能需要在回呼函式中創建更多測驗來處理 int 值,但現在它處理字串和陣列。我只是使用搜索物件中的第一個值卡在回圈上。
uj5u.com熱心網友回復:
如果其中任何一個不匹配,您需要回圈SearchParams
并回傳。false
成功回圈完所有這些后,回傳true
。
const search = {Condition: "Allergies", ProductName: "Acetaminophen"}
const meds = [{Active: false,
Condition: ["Allergies"],
DaysSupply: 0,
Doseform: "Tablet",
ProductName: "Acetaminophen 650 mg tablet, extended release",
Protocol: "PATIENT COMMUNICATION: Price $10. 100 tablets",
Quantity: 100},{Active: false,
Condition: ["COVID-19 Symptoms"],
DaysSupply: 0,
Doseform: "Tablet",
ProductName: "Acetaminophen 650 mg tablet, extended release",
Protocol: "PATIENT COMMUNICATION: Price is $10. 100 tablets",
Quantity: 100},{Active: false,
Condition: ["Headaches (Migraine & Tension)"],
DaysSupply: 0,
Doseform: "Tablet",
ProductName: "Acetaminophen 650 mg tablet, extended release",
Protocol: "PATIENT COMMUNICATION: Price is $10. 100 tablets",
Quantity: 100},{Active: false,
Condition: ["Allergies"],
DaysSupply: 0,
Doseform: "Capsule",
ProductName: "Amoxicillin 500 mg capsule",
Protocol: "PATIENT COMMUNICATION: Price is $20- 21 capsules",
Quantity: 21}]
const comparedtosearch = (medication, searchParams) => {
for (let [key, value] of Object.entries(searchParams)){
// do(es) the object(s) contain the search value
if(key === "Condition"){
const includesSearch = (item) => item.toLowerCase().includes(value.toLowerCase())
// conditions are arrays
if (!medication[key].some(includesSearch)) {
return false
}
} else if(key != "Condition"){
if (!medication[key].toLowerCase().includes(value.toLowerCase())) {
return false
}
}
}
return true
}
const handleSearch = (searchParams) => {
// clean copy of the medications (formularies)
const medications = [...meds]
const formFiltered = medications.filter((med) => comparedtosearch(med, searchParams))
console.log(formFiltered)
//setLoading(false)
}
handleSearch(search)
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/504534.html
標籤:javascript 数组 for循环 目的 筛选