如何在JavaScript中按status_state=complete过滤嵌套数组
问题描述
我有一个如下结构的动态嵌套数组:
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 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" } ] ] ]
解决方案
你的代码存在两个核心问题:
- 字段名错误:对象里的状态字段是
status_state,不是status - 仅筛选了外层数组,未对每个分类下的子数组做筛选,也没过滤掉没有
complete项的分类
正确实现需要两步操作:
- 对每个分类下的子数组,筛选出
status_state === "complete"的项 - 过滤掉筛选后子数组为空的分类
代码实现如下:
const filteredResult = array .map(item => { // 筛选当前分类下状态为complete的子项 const filteredSubItems = item[1].filter(subItem => subItem.status_state === "complete"); // 返回新的分类数组,替换子数组为筛选后的结果 return [item[0], filteredSubItems]; }) // 过滤掉筛选后子数组为空的分类 .filter(item => item[1].length > 0);
代码说明
map方法:遍历外层数组的每个分类,对每个分类的子数组执行filter,只保留符合状态要求的对象,生成包含原分类名称和筛选后子数组的新数组。filter方法:剔除那些筛选后子数组为空的分类,确保最终结果仅包含有complete项的分类。
执行这段代码后,就能得到你期望的筛选结果。
内容的提问来源于stack exchange,提问作者Khaney423
相关产品推荐
相关产品推荐

