【发布时间】:2021-10-21 23:06:54
【问题描述】:
这是我的数据:
{
"productGroups": [
{
"selectedProducts": [
{ "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e99f9b13b4126a9e07e36" }
]
},
{
"selectedProducts": [
{ "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
]
},
{
"selectedProducts": [
{ "productPricing": { "recurringFee": {}, "oneTimeFee": {} } }
]
}
]
}
如果没有 selectedProducts.id,我想删除数组元素
所以结果应该是:
{
"productGroups": [
{
"selectedProducts": [
{ "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e99f9b13b4126a9e07e36" }
]
}
]
}
这是我尝试过的:
const filteredData = {
productGroups: data.productGroups.map(productGroup => {
const selectedProduct = productGroup.selectedProducts?.filter(product => product.id);
return selectedProduct;
}),
};
我的结果是错误的,我得到的结果是空数组:
{
"productGroups": [
[
{ "productPricing": { "recurringFee": {}, "oneTimeFee": {} }, "id": "610e9a5eb13b4126a9e07e37" }],
[],
[]
]
}
【问题讨论】:
标签: javascript reactjs ecmascript-6