简体   繁体   English

将一个数组中具有相同 id 的所有对象添加到另一个数组中具有相同 id 的 object 的数组中

[英]Add all objects with the same id from one array into an array of an object with the same id in another array

I have 2 arrays. I need to move all the objects that have the same id in arraySecondary into an array inside an object with the same id in arrayPrimary.我有 2 个 arrays。我需要将 arraySecondary 中具有相同 id 的所有对象移动到 arrayPrimary 中具有相同 id 的 object 中的数组中。

Example:例子:

    const arrayPrimary = [
      { "id": "1", "location": "France", "price": "12,3" },
      { "id": "2", "location": "Germany", "price": "12,0" },
      { "id": "3", "location": "USA", "price": "10" },
      { "id": "4", "location": "Italy", "price": "16" },
    ];

    const arraySecondary = [
      { "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" },
      { "id": "2", "location": "Paris", "dd": "lql" },
      { "id": "3", "location": "Egypt" },
      { "id": "2", "name": "joe", "location": "London" },
      { "id": "1", "location": "location", "name": "april" },
      { "id": "2", "name": "mei", "location": "Barcelona" },
    ];

Expected result:预期结果:

    [
      {
        id: 1,
        location: "France",
        price: "12,3",
        area: [
          { id: 1, location: "location", name: "april" },
          { id: 1, name: "phil", location: "New York", price: "1,3", dd: "lql" },
        ],
      },
      {
        id: 2,
        location: "Germany",
        price: "12,0",
        area: [
          { id: 2, location: "Paris", dd: "lql" },
          { id: 2, name: "joe", location: "London" },
          { id: 2, name: "mei", location: "Barcelona" },
        ],
      },
      { id: 3, location: "USA", price: 10, area: [{ id: 3, location: "Egypt" }] },
      { id: 4, location: "Italy", price: 16 },
    ];
    //or json

First i add an empty array to each object in arrayPrimary.首先,我向 arrayPrimary 中的每个 object 添加一个空数组。

 arrayPrimary.map((v) => ({ ...v, area: [] }));

After that i filter arraySecondary by id and push all the results into area array in each object in arrayPrimary.之后,我通过 id 过滤 arraySecondary,并将所有结果推送到 arrayPrimary 中每个 object 的区域数组中。 But here i get stuck.但是在这里我被卡住了。

    console.log(
      arrayPrimary.forEach((main) =>
        main.area.push(arraySecondary.filter((items) => items.id === main.id))
      )
    );

Second idea is to first order each object in arraySecondary by id and then push that into empty area array in arrayPrimary第二个想法是首先按 id 对 arraySecondary 中的每个 object 进行排序,然后将其推入 arrayPrimary 中的空区域数组

let op = arrayItems.reduce((op,inp) => {
  op[inp.id] = op[inp.id] || []
  op[inp.id].push(inp)
  return op
},{})

console.log(op)

And this is where i am stuck with both ideas.这就是我坚持这两个想法的地方。

something like this?是这样的吗?

 const arrayPrimary = [ { "id": "1", "location": "France", "price": "12,3" }, { "id": "2", "location": "Germany", "price": "12,0" }, { "id": "3", "location": "USA", "price": "10" }, { "id": "4", "location": "Italy", "price": "16" }, ]; const arraySecondary = [ { "id": "1", "name": "phil", "location": "New York", "price": "1,3", "dd": "lql" }, { "id": "2", "location": "Paris", "dd": "lql" }, { "id": "3", "location": "Egypt" }, { "id": "2", "name": "joe", "location": "London" }, { "id": "1", "location": "location", "name": "april" }, { "id": "2", "name": "mei", "location": "Barcelona" }, ]; const composed = arrayPrimary.map(d => { return {...d, area: arraySecondary.filter(({id}) => d.id === id) } }) console.log(composed)

You can do it as followed:您可以按如下方式进行:

const arrayResut=arrayPrimary.map(primaryElement=>{
   primaryElement.area=arraySecondary.filter(secondaryElement=>secondaryElement.id==primaryElement.id);
   return primaryElement;
})

This will create a copy of arrayPrimary , and in the process add an area key which will contain all the elements of arraySecondary if the id of the arraySecondary element is equal to the id of the arrayPrimary element.这将创建arrayPrimary的副本,并在此过程中添加一个area键,如果 arraySecondary 元素的 id 等于arraySecondary元素的 id,它将包含arraySecondaryarrayPrimary元素。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何检查对象数组中的 ID 是否与另一个对象数组中的 ID 相同? - How to check if the ID in an array of objects is the same as the ID in another array of objects? 使用具有相同ID的对象添加数组分数 - Add scores of array with objects that have the same ID JavaScript将具有相同ID的对象转换为对象数组 - JavaScript converting object with same id to array of objects 合并数组中具有相同id的对象 - Merge objects with same id in array 数组中的所有对象都以相同的值更新,同时将具有唯一ID的对象推入数组中 - all objects in array are updating with same value while pushing the object with unique id into an array 如果数组中的对象值 `num` 大于 `quota`,则将类添加到我的元素中,该元素与我的数组对象具有相同的 #id - If object value `num` is greater than `quota` in array, then add class to my element that has the same #id from my array objects 过滤具有相同 id 的对象数组 - filter an array of object with same id 具有相同 ID 的对象被推入相同的数组 - Objects with same ID pushed into same array 循环遍历对象数组并添加具有相同 id 的对象 - Loop through array of objects and add objects that have the same id 比较两个不同的对象数组并仅从一个数组中过滤出具有相同 ID 的项目 - compare two different array of objects and filter out the item which is having same ID from one array only
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM