[英]How to filter an array of arrays in JavaScript
我有一個看起來像的動態數組
var array = [
[
"Advance Payment",
[
{
"key": "100001",
"reason_name": "Tax ID is missing",
"reason_category": "Advance Payment",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100002",
"reason_name": "Bank account information mismatch",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
.
.
.,
{
"key": "100003",
"reason_name": "Invoice Settings Error",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
[
"Parental Authorization",
[
{
"key": "100004",
"reason_name": "Missing form",
"reason_category": "Parental Authorization",
"status_state": "required",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2022-02-08T22:22:49.355Z"
}
]
],
[
"Missing Credit Card Info",
[
{
"key": "100005",
"reason_name": "Invalid Credit Card Numnber",
"reason_category": "Missing Credit Card Info",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100006",
"reason_name": "Expired Credit Card",
"reason_category": "Missing Credit Card Info",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100007",
"reason_name": "Missing Signature on File",
"reason_category": "Missing Credit Card Info",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100008",
"reason_name": "Invalid CVV entered",
"reason_category": "Missing Credit Card Info",
"status_state": "required",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
.
.
.
,
[
"Missing Home Address",
[
{
"key": "100009",
"reason_name": "Invalid Postal Code",
"reason_category": "Missing Home Address",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100010",
"reason_name": "Missing Address Line 1",
"reason_category": "Missing Home Address",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
.
.
.,
{
"key": "100011",
"reason_name": "State Code Missing",
"reason_category": "Missing State Code",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
]
]
我想根據“status_state”===“complete”過濾這個數組以返回如下內容:
var filteredResult = [
[
"Advance Payment",
[
{
"key": "100002",
"reason_name": "Bank account information mismatch",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100003",
"reason_name": "Invoice Settings Error",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
[
"Missing Credit Card Info",
[
{
"key": "100006",
"reason_name": "Expired Credit Card",
"reason_category": "Missing Credit Card Info",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
[
"Missing Home Address",
[
{
"key": "100010",
"reason_name": "Missing Address Line 1",
"reason_category": "Missing Home Address",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100011",
"reason_name": "State Code Missing",
"reason_category": "Missing State Code",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
]
]
我如何實現這一目標?
我試過了
const filteredResult = array.filter((item) =>
item[1].some((subItem) => subItem.status === "complete"
然而,這也包括我不想要的數據...... filteredResult 看起來像
[
[
"Advance Payment",
[
{
"key": "100001",
"reason_name": "Tax ID is missing",
"reason_category": "Advance Payment",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100002",
"reason_name": "Bank account information mismatch",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100003",
"reason_name": "Invoice Settings Error",
"reason_category": "Advance Payment",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
[
"Missing Credit Card Info",
[
{
"key": "100005",
"reason_name": "Invalid Credit Card Numnber",
"reason_category": "Missing Credit Card Info",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100006",
"reason_name": "Expired Credit Card",
"reason_category": "Missing Credit Card Info",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100007",
"reason_name": "Missing Signature on File",
"reason_category": "Missing Credit Card Info",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100008",
"reason_name": "Invalid CVV entered",
"reason_category": "Missing Credit Card Info",
"status_state": "required",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
],
[
"Missing Home Address",
[
{
"key": "100009",
"reason_name": "Invalid Postal Code",
"reason_category": "Missing Home Address",
"status_state": "pending",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100010",
"reason_name": "Missing Address Line 1",
"reason_category": "Missing Home Address",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
},
{
"key": "100011",
"reason_name": "State Code Missing",
"reason_category": "Missing State Code",
"status_state": "complete",
"update_at": "2022-04-08T22:22:49.355Z",
"date_created": "2021-12-08T22:22:49.355Z"
}
]
]
]
由於您想隨身攜帶密鑰,因此您可以映射結果並以相同的結構返回它,但對內部項目進行過濾。
const filteredResult = array.map(([key, nestedArray]) => [key, nestedArray.filter(item => item.status_state === "complete")]);
我會過濾兩次以獲得您想要的結果。 一次在內部項目上(映射數組),一次在外部數組上:
array.map(([category, issues]) => [category, issues.filter(({status_state}) => status_state === "complete")]).filter(([, issues]) => issues.length);
這是一個利用flatMap
同時對給定數組進行map
和filter
的解決方案:
array.flatMap(([category, lines]) => {
const completedLines = lines.filter(x => x.status_state === 'complete');
if (completedLines.length === 0) {
return [];
} else {
return [[category, completedLines]]; // Notice the double brackets for flatMap
}
});
使用reduce()
這樣我們就可以在filter()
找不到任何東西時刪除整個數組:
var array = [["Advance Payment", [{"key": "100001", "reason_name": "Tax ID is missing", "reason_category": "Advance Payment", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100002", "reason_name": "Bank account information mismatch", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100003", "reason_name": "Invoice Settings Error", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Parental Authorization", [{"key": "100004", "reason_name": "Missing form", "reason_category": "Parental Authorization", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2022-02-08T22:22:49.355Z"} ] ], ["Missing Credit Card Info", [{"key": "100005", "reason_name": "Invalid Credit Card Numnber", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100006", "reason_name": "Expired Credit Card", "reason_category": "Missing Credit Card Info", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100007", "reason_name": "Missing Signature on File", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100008", "reason_name": "Invalid CVV entered", "reason_category": "Missing Credit Card Info", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Missing Home Address", [{"key": "100009", "reason_name": "Invalid Postal Code", "reason_category": "Missing Home Address", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100010", "reason_name": "Missing Address Line 1", "reason_category": "Missing Home Address", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100011", "reason_name": "State Code Missing", "reason_category": "Missing State Code", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ] ] const res = array.reduce((p, [ key, value ]) => { let filtered = value.filter(o => o.status_state === 'complete'); return (filtered.length)? [...p, [ key, filtered ] ]: p; }); console.log(res)
只需映射數組並返回key
和過濾后的array
:
var array = [["Advance Payment", [{"key": "100001", "reason_name": "Tax ID is missing", "reason_category": "Advance Payment", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100002", "reason_name": "Bank account information mismatch", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100003", "reason_name": "Invoice Settings Error", "reason_category": "Advance Payment", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Parental Authorization", [{"key": "100004", "reason_name": "Missing form", "reason_category": "Parental Authorization", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2022-02-08T22:22:49.355Z"} ] ], ["Missing Credit Card Info", [{"key": "100005", "reason_name": "Invalid Credit Card Numnber", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100006", "reason_name": "Expired Credit Card", "reason_category": "Missing Credit Card Info", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100007", "reason_name": "Missing Signature on File", "reason_category": "Missing Credit Card Info", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100008", "reason_name": "Invalid CVV entered", "reason_category": "Missing Credit Card Info", "status_state": "required", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ], ["Missing Home Address", [{"key": "100009", "reason_name": "Invalid Postal Code", "reason_category": "Missing Home Address", "status_state": "pending", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100010", "reason_name": "Missing Address Line 1", "reason_category": "Missing Home Address", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"}, {"key": "100011", "reason_name": "State Code Missing", "reason_category": "Missing State Code", "status_state": "complete", "update_at": "2022-04-08T22:22:49.355Z", "date_created": "2021-12-08T22:22:49.355Z"} ] ] ] const res = array.map(([key, nestedArr]) => [key, nestedArr.filter((arr) => arr.status_state === 'complete')]) console.log(res)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.