我面臨一個問題,我不明白如何解決它。
我有一項服務,我回傳以下 JSON:
{
"data": [
{
"data": 179,
"data_form": "Finished - Form Covid-19 Test 1",
"form": 144,
"user": 1,
"status": "Finished"
},
{
"data": 176,
"data_form": "In process - Form Covid-19 Test 2",
"form": 144,
"user": 1,
"status": "Finished"
},
{
"data": 177,
"data_form": "Finished - Form Covid-19 Test 3",
"form": 144,
"user": 1,
"status": "In process"
},
{
"data": 178,
"data_form": "Finished - Form Covid-19 Test 4",
"form": 144,
"user": 1,
"status": "In process"
}
]
}
我正在使用一個 PIPE,向它發送我的陣列、我要過濾的值以及該值所在的列:
<ion-list
*ngFor="let form of forms | filter: searchValue: ['status','data_form','data']">
<app-form-user [form]="form">
</app-form-user>
</ion-list>
如果我指出該列,它會起作用:
return array.filter(
//It works
item=> item[column[0]].toLowerCase().includes( text )
);
這個想法是在您向管道指示的所有列中查找值,我嘗試了以下操作:
1.
return array.filter(
item=> item[column.forEach((data) => found = data )].toLowerCase().includes( text );
);
return array.filter(
item=>Object.keys(column).every(key =>
item[key] == column[key]
)
);
return array.filter(
item=> column.forEach((data) => item[data].toLowerCase().includes( text ))
);
我希望你能幫助我,謝謝。
uj5u.com熱心網友回復:
無需通過列即可作業,搜索所有可用列。
一行代碼
此過濾器通過您必須檢查值并回傳結果的每個鍵。
詳細解釋:
1) Base is the object you posted and assuming you want to filter the "data"
2) First filter goes thru all the elements in the array
3) second filter goes thru all the keys in the object which is element in the array
4) Checking if the column has the data
5) returns the array of elements if matched
const text = 'Test 2';
return array.filter(v=> Object.keys(v).filter(key=> v[key].toString().toLowerCase().includes(text.toString().toLowerCase())).length > 0)
結果是:

uj5u.com熱心網友回復:
我將使用 .map() 遍歷每個專案的每個鍵,并確定它是否找到特定值,并在過濾器級別回傳它
return array.filter(item => {
let found = false;
Object.keys(item).map(x => {
found = found || item[x].toLowerCase().includes( text );
});
return found;
});
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/443228.html
標籤:javascript 数组 json 有角度的 筛选
上一篇:如何使用useState函陣列件更新物件陣列中的陣列
下一篇:陣列元素在bash中的所有組合
