JavaScript过滤三级嵌套数组:获取含指定生效日期的gList元素
筛选三级嵌套JSON中的指定gList元素
我有一组三级嵌套的JSON数据,需要筛选出所有满足条件的gList元素——该元素的productList数组中存在effectiveDate为"12/12/2023"的项,也可以只获取首个匹配结果。
示例数据
var data = {"cList": [{"Id": "111111", "Number": "176116", "Name": "Test", "gList": [{"gNumber": "123456", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "12/12/2023", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023", "partB": false, "id": ""}]}, {"gNumber": "10422798", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "01/01/2022", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023", "partB": false, "id": ""}]}, {"gNumber": "10422700", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "01/01/2023", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023"}]}]}, {"Id": "222", "Number": "176116", "Name": "Test", "gList": [{"gNumber": "1234567", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "12/12/2010", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023", "partB": false, "id": ""}]}, {"gNumber": "10422798", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "01/01/2022", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023", "partB": false, "id": ""}]}, {"gNumber": "10422795", "productList": [{"spn": "2118593162", "pno": "2118593163", "effectiveDate": "12/12/2023", "partB": false, "id": ""}, {"spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": ""}, {"spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023"}]}]}]};
尝试的错误代码
const filteredClientList = data.cList.filter( (cListElement) => cListElement.gList.find( (gListElement) => gListElement.productList.filter((productListElement) => productListElement.effectiveDate === "12/12/2023"), ), ); console.log("filteredClientList", JSON.stringify(filteredClientList));
预期输出
[ { "gNumber": "123456", "productList": [ { "spn": "2118593162", "pno": "2118593163", "effectiveDate": "12/12/2023", "partB": false, "id": "" }, { "spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": "" }, { "spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023", "partB": false, "id": "" } ] }, { "gNumber": "10422795", "productList": [ { "spn": "2118593162", "pno": "2118593163", "effectiveDate": "12/12/2023", "partB": false, "id": "" }, { "spn": "2131493390", "pno": "2131493390", "effectiveDate": "01/01/2020", "partB": false, "id": "" }, { "spn": "2100171794", "pno": "2100171794", "benefitPlanType": "Wellness Programs", "effectiveDate": "01/01/2023" } ] } ]
问题分析与解决方案
你的代码有两个核心问题:
- 最终返回的是
cList元素,而非直接返回符合条件的gList元素,和预期输出结构不符; - 判断productList时用了
filter,它会返回一个数组,虽然非空数组在布尔判断中为true,但some方法更适合这个场景——它只要找到第一个匹配项就会停止遍历,效率更高。
方案1:获取所有符合条件的gList元素
const filteredGLists = data.cList.flatMap(cItem => // 对每个cItem的gList进行筛选,保留productList中存在指定日期的项 cItem.gList.filter(gItem => gItem.productList.some(product => product.effectiveDate === "12/12/2023") ) ); console.log("filteredGLists", JSON.stringify(filteredGLists, null, 2));
方案2:仅获取首个匹配的gList元素
const firstMatch = data.cList // 先把所有cItem中的gList展开成一个一维数组 .flatMap(cItem => cItem.gList) // 找到第一个符合条件的gList元素 .find(gItem => gItem.productList.some(product => product.effectiveDate === "12/12/2023") ); console.log("firstMatch", JSON.stringify(firstMatch, null, 2));
内容的提问来源于stack exchange,提问作者RSKMR
相关产品推荐
相关产品推荐

