You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何将两个Fetch响应数据集通过左连接合并为单一数据集?

左连接合并两个数据集实现方案

你可以通过遍历table1的每一条数据,匹配table2中符合oldname = table1.name条件的数据,实现左连接合并。具体代码如下:

Promise.all([
  fetch(table1URL).then(response => response.json()),
  fetch(table2URL).then(response => response.json())
]).then(response => {
  let table1 = response[0];
  let table2 = response[1];
           
  // 实现左连接合并
  let merged_data = table1.map(item1 => {
    // 找到table2中匹配的项
    const matchedItem = table2.find(item2 => item2.oldname === item1.name);
    // 构造合并后的字段
    return {
      name: item1.name,
      modifiedDateTime: item1.modifiedDateTime,
      modifiedBy: item1.modifiedBy,
      Modified: matchedItem ? matchedItem.Modified : null,
      oldname: matchedItem ? matchedItem.oldname : null
    };
  });

  // 这里可以使用merged_data做后续操作
  console.log(merged_data);
})

逻辑说明

  • 左连接保留table1的所有数据,即使table2中没有匹配项,对应table2的字段会设为null
  • 使用map遍历table1的每条数据,通过find在table2中查找符合关联条件的记录
  • 严格按照你指定的字段构造合并后的数据集

内容的提问来源于stack exchange,提问作者Gangrel

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.11 12:20:27