多维数组与 JavaScript 中的一个数组通过特定键组合

问题描述 投票:0回答:1

我有一个如下所示的数组

[
 {
"__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);
}, []);
javascript arrays react-native merge array-merge
1个回答
0
投票
const results = getRelatedData.flatMap(el => el.crosssell_products);
© www.soinside.com 2019 - 2024. All rights reserved.