js表格实现拼接
表格拼接方法
合并行数据
使用 concat() 方法合并两个表格的行数据
const table1 = [
{id: 1, name: 'Alice'},
{id: 2, name: 'Bob'}
];
const table2 = [
{id: 3, name: 'Charlie'},
{id: 4, name: 'David'}
];
const mergedTable = table1.concat(table2);
合并列数据
使用 map() 和展开运算符合并表格的列

const tableA = [
{id: 1, age: 25},
{id: 2, age: 30}
];
const tableB = [
{id: 1, department: 'HR'},
{id: 2, department: 'IT'}
];
const joinedTable = tableA.map(item => ({
...item,
...tableB.find(bItem => bItem.id === item.id)
}));
动态列合并 处理不同结构的表格合并

function mergeTables(table1, table2, key) {
return table1.map(item => {
const match = table2.find(i => i[key] === item[key]);
return match ? {...item, ...match} : item;
});
}
数组展开方式 使用展开运算符合并多个表格
const combined = [...table1, ...table2, ...table3];
注意事项
- 确保合并键的唯一性
- 处理可能存在的空值情况
- 考虑性能优化当处理大型数据集
- 注意保留原始数据不变性
以上方法可根据实际需求组合使用,实现更复杂的表格拼接逻辑。






