如何在 ReactJS 中合并两个对象数组?

How to merge two arrays of objects in ReactJS?

我有两个对象数组,我需要将它们合并在一起才能只得到一个对象数组。 对象很多,都超过500+对象

这是两个对象数组的结构示例:

let API = [
  {
    actif: true,
    id: 8,
    creation_user: "David",
    date_creation: "févr  4 2022 12:17PM",
    description: "A description",
    version: "v1r1"
  },
  {
    actif: true,
    id: 10,
    creation_user: "Julien",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  },
  {
    actif: false,
    id: 20,
    creation_user: "Tom",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  }
]

let Parameters = [
  {
    id: 10,
    name: "codeRR",
    type: "string"
  },
  {
    id: 20,
    name: "codeAA",
    type: "string"
  },
  {
    id: 20,
    name: "codeCC",
    type: "string"
  }
]

我们可以看到,它们都有一个ID,这个ID在两个数组中都是一样的。 但不是每次,API 可以没有参数。

我想要实现的是进入第二个数组,“名称”和“类型”,并将其添加到第一个数组(或新数组)。

我已经尝试在第一个数组上使用 .map(也尝试过第二个数组):

let Ressource = api.map(item => Object.assign({}, item, parametre.find(target => target.id === item.id)));
 
 // Result of Ressource
 
 [
  {
    actif: true,
    id: 8,
    creation_user: "David",
    date_creation: "févr  4 2022 12:17PM",
    description: "A description",
    version: "v1r1";
    name: "codeRR",
    type: "string
  }
 ]
 
 // Want I want :
 [
  {
    actif: true,
    id: 8,
    creation_user: "David",
    date_creation: "févr  4 2022 12:17PM",
    description: "A description",
    version: "v1r1",
    parameter: [
      {
        name: "codeRR",
        type: "string
      },
      {
        name: "codePP",
        type: "string
      }
    ]
  }
 ]

但是当 API 有多个参数时,它会覆盖前一个。 是否可以在我的 API

中制作类似的东西
parameter: [{"name": "codeRR", type: "string"}, {"name": "codePP", "type": "string"}]
  1. 使用 Array.prototype.reduceidParameters 数组进行分组,这将给出一个对象,其中的键是 id 和值是参数对象的数组。

  2. 遍历 API 数组并使用上面创建的对象在参数信息可用的地方添加 parameter 属性。

let 
  API = [
    { actif: true, id: 8, creation_user: "David", date_creation: "févr  4 2022 12:17PM", description: "A description", version: "v1r1" },
    { actif: true, id: 10, creation_user: "Julien", date_creation: "févr  10 2022 12:17PM", description: "A description", version: "v1r2" },
    { actif: false, id: 20, creation_user: "Tom", date_creation: "févr  10 2022 12:17PM", description: "A description", version: "v1r2" },
  ],
  Parameters = [
    { id: 10, name: "codeRR", type: "string" },
    { id: 20, name: "codeAA", type: "string" },
    { id: 20, name: "codeCC", type: "string" },
  ],
  ParametersGroup = Parameters.reduce((r, p) => ((r[p.id] ??= []).push(p), r), {}),
  APIWithParams = API.map((o) => ({
    ...o,
    ...(ParametersGroup[o.id] && { parameters: ParametersGroup[o.id] }),
  }));

console.log(APIWithParams);

如果想对单个参数和多个参数进行不同的处理,请参考下面的解决方案:

let 
  API = [
    { actif: true, id: 8, creation_user: "David", date_creation: "févr  4 2022 12:17PM", description: "A description", version: "v1r1" },
    { actif: true, id: 10, creation_user: "Julien", date_creation: "févr  10 2022 12:17PM", description: "A description", version: "v1r2" },
    { actif: false, id: 20, creation_user: "Tom", date_creation: "févr  10 2022 12:17PM", description: "A description", version: "v1r2" },
  ],
  Parameters = [
    { id: 10, name: "codeRR", type: "string" },
    { id: 20, name: "codeAA", type: "string" },
    { id: 20, name: "codeCC", type: "string" },
  ],
  ParametersGroup = Parameters.reduce((r, p) => ((r[p.id] ??= []).push(p), r), {}),
  APIWithParams = API.map((o) => {
    const paramsForId = ParametersGroup[o.id];
    return {
      ...o,
      ...(paramsForId && (paramsForId.length > 1 ? { parameters: paramsForId } : paramsForId[0])),
    };
  });

console.log(APIWithParams);

注意: 一次又一次地寻找 id 的参数是 不是 最优的。因此我们预先创建了一个对象,并在添加参数信息时引用了它。

其他相关文件:

如果参数具有相同的属性,它们将被覆盖。

如果您同时需要它们,则需要将它们存储在一个数组中

像这样

const decorate = (data, parameters) => data.map(d => ({...d, parameters: parameters.filter(({id}) => d.id === id)}))




let API = [
  {
    actif: true,
    id: 8,
    creation_user: "David",
    date_creation: "févr  4 2022 12:17PM",
    description: "A description",
    version: "v1r1"
  },
  {
    actif: true,
    id: 10,
    creation_user: "Julien",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  },
  {
    actif: false,
    id: 20,
    creation_user: "Tom",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  }
]

let Parameters = [
  {
    id: 10,
    name: "codeRR",
    type: "string"
  },
  {
    id: 20,
    name: "codeAA",
    type: "string"
  },
  {
    id: 20,
    name: "codeCC",
    type: "string"
  }
]

console.log(decorate(API,Parameters ))

你快到了。您可以为 map 添加额外的逻辑来分别处理多个和单个参数。

let API = [{
    actif: true,
    id: 8,
    creation_user: "David",
    date_creation: "févr  4 2022 12:17PM",
    description: "A description",
    version: "v1r1"
  },
  {
    actif: true,
    id: 10,
    creation_user: "Julien",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  },
  {
    actif: false,
    id: 20,
    creation_user: "Tom",
    date_creation: "févr  10 2022 12:17PM",
    description: "A description",
    version: "v1r2"
  }
]

let Parameters = [{
    id: 10,
    name: "codeRR",
    type: "string"
  },
  {
    id: 20,
    name: "codeAA",
    type: "string"
  },
  {
    id: 20,
    name: "codeCC",
    type: "string"
  }
]

const result = API.map((data) => {
  const updatedData = Object.assign({}, data)
  const foundParameters = Parameters.filter(({
    id
  }) => id === data.id)
  if (foundParameters.length === 0) {
    return data
  }

  if (foundParameters.length === 1) {
    return Object.assign(updatedData, foundParameters[0])
  } else {
    return Object.assign(updatedData, {
      parameter: foundParameters.map(({
        name,
        type
      }) => ({
        name,
        type
      }))
    })
  }
});

console.log(result)