JavaScript Filter对象的第3级嵌套数组

mkshixfv  于 2023-04-10  发布在  Java
关注(0)|答案(1)|浏览(119)

我有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"
      }
    ]
  }
]
yfwxisqw

yfwxisqw1#

flatMap后面是filter,内部是some
flatMap相当于执行map,然后执行flat()
flatMap获取[{gNumber,productList},...]中的所有数据。现在要过滤它,条件必须至少有一个productList应该具有给定的effectiveDate。为此,使用some

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 filtered = data.cList.flatMap(x => x.gList).filter(y => y.productList.some(z => z.effectiveDate === '12/12/2023') )

console.log(filtered)
.as-console-wrapper { max-height: 100% !important; top: 0; }

相关问题