可以使用 Array.reduce()
函数
将多个 JSON 数组对象合并为一个。以下是 TypeS
cr
i
pt
代码示例:
interface Person {
name: string;
age: number;
const people1: Person[] = [
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 25 },
const people2: Person[] = [{ name: 'Charlie', age: 30 }];
const mergedPeople: Person[] = [people1, people2].reduce((prev, current) => [...prev, ...current], []);
console.log(mergedPeople);
输出结果为:
{ name: 'Alice', age: 20 },
{ name: 'Bob', age: 25 },
{ name: 'Charlie', age: 30 }
通过 reduce() 函数,将 people1 和 people2 数组对象合并为一个 mergedPeople 数组对象。