将数组中的元素合并到具有相同键的子数组中

Merging elements in an array in a subarray with same key

我真的很困惑为什么这种方法不起作用。我有两个数组,所以我遍历主数组然后比较键以复制数组中的相应记录。这是数据示例:

let r2 = [
    {
        "record_guid": "cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392",
        "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit," 
    },
    {
        "record_guid": "9f0689e7-1fff-4731-9e60-35c80b62fd33",
        "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit," 
    },
    {
        "record_guid": "b61c72a8-5c83-485e-8366-6f4a56d28b55",
        "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit," 
    }
];

let r1= [
  {
    "record_guid": "5e3dc291-662d-4ca0-895b-6272d51156fb",
    "record_id": 1,
    "parent_record_id": null,
    "description": "Introduction",
    "sub_records": [
        {
            "record_guid": "cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392",
            "record_id": 10,
            "parent_record_id": 1,
            "description": "Background",
            "sub_records": [],
        },
        {
            "record_guid": "9f0689e7-1fff-4731-9e60-35c80b62fd33",
            "record_id": 11,
            "parent_record_id": 1,
            "description": "Details",
            "sub_records": [],
        },
        {
            "record_guid": "b61c72a8-5c83-485e-8366-6f4a56d28b55",
            "record_id": 12,
            "parent_record_id": 1,
            "description": "Regulations",
            "sub_records": [],
        },
        
            ],
        }
    ],
}
];

像这样的东西应该可以工作:

const mergeById = (r1, r2) =>
    r1.map(itm => ({
        ...r2.find((item) => (item.record_guid === itm.record_guid) && item),
        ...itm
    }));

console.log(mergeById(r1, r2));

或其他类似版本:

let merged = [];

for(let i=0; i<r1.length; i++) {
  merged.push({
   ...r1[i], 
   ...(r2.find((itmInner) => itmInner.record_guid === r1[i].record_guid))}
  );
}
 
console.log(merged);

我只是没有得到相应数组元素中的注释字段。您知道为什么无法将第二个数组复制到第一个数组以获得如下所示的内容吗?

[
  {
    "record_guid": "5e3dc291-662d-4ca0-895b-6272d51156fb",
    "record_id": 1,
    "parent_record_id": null,
    "description": "Introduction",
    "sub_records": [
        {
            "record_guid": "cb7fba9b-e8fc-40c8-9a2c-9ba2e06d1392",
            "record_id": 10,
            "parent_record_id": 1,
            "description": "Background",
            "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
            "sub_records": [],
        },
        {
            "record_guid": "9f0689e7-1fff-4731-9e60-35c80b62fd33",
            "record_id": 11,
            "parent_record_id": 1,
            "description": "Details",
            "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
            "sub_records": [],
        },
        {
            "record_guid": "b61c72a8-5c83-485e-8366-6f4a56d28b55",
            "record_id": 12,
            "parent_record_id": 1,
            "description": "Regulations",
            "comment": "Lorem ipsum dolor sit amet, consectetur adipiscing elit,"
            "sub_records": [],
        },
        
            ],
        }
    ],
}
]

根据输出结果,sub_records 需要映射并且展开运算符应该在 find 方法的结果之前展开原始对象。

const mergeById = (r1, r2) =>
r1.map(itm => {
 itm.sub_records = itm.sub_records.map(sub_item => (
     { ...sub_item,
         ...r2.find(r2_item => r2_item.record_guid === sub_item.record_guid),
     }
    )
    )
  
   return itm 
})

console.log(mergeById(r1,r2))