我有json数据,并试图过滤嵌套数组对象,其中有在第三级。
样本数据:
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"
},
],
}
]
}
],
};
我正在尝试获取productList数组中唯一有效日期为“12/12/2023”的gNumber;
或
得到第一个匹配结果也可以。
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"
}
]
}
]
1条答案
按热度按时间yfwxisqw1#
flatMap
后面是filter
,内部是some
。flatMap
相当于执行map
,然后执行flat()
。flatMap
获取[{gNumber,productList},...]
中的所有数据。现在要过滤它,条件必须至少有一个productList
应该具有给定的effectiveDate
。为此,使用some