我有一个如下所示的数组
[
{
"__typename": "ConfigurableProduct",
"uid": "MzY3",
"name": "AISLING BODYSUIT",
"crosssell_products": [
{
"__typename": "ConfigurableProduct",
"uid": "NTI=",
"name": "Product 1"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 2"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 3"
}
]
},
{
"__typename": "ConfigurableProduct",
"uid": "MzI4",
"name": "CERYS BODYSUIT",
"crosssell_products": [
{
"__typename": "ConfigurableProduct",
"name": "Product 4"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 5"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 6"
}
]
}
]
我想要包含 crosssell_products 数组的数据的单个数组,这意味着我想要以下格式
"crosssell_products": [
{
"__typename": "ConfigurableProduct",
"uid": "NTI=",
"name": "Product 1"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 2"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 3"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 4"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 5"
},
{
"__typename": "ConfigurableProduct",
"name": "Product 6"
},
]
我尝试了下面的代码但不起作用
let aa = getRelatedData?.products?.items.reduce(function (a, b) {
return a.concat(b);
}, []);
const results = getRelatedData.flatMap(el => el.crosssell_products);