合并两个具有不同键索引的JSON数组:Typescript

时间:2018-09-28 13:43:05

标签: json typescript

我有两个JSON数组,

A:

[ {"id": 123, "name": "AnyName1", "type": "AnyType"},
{"id": 231, "name": "AnyName2", "type": "AnyType"} ]

B:

[ {"id": 569, "thing": "AnyThing3"},
{"id": 891, "thing": "AnyThing4"} ]

我想将这两个数组(与id无关)合并到一个普通的JSON数组中:

C = A + B:

[ {"id": 123, "name": "AnyName1", "type": "AnyType"},
{"id": 231, "name": "AnyName2", "type": "AnyType"},
{"id": 569, "thing": "AnyThing3"},
{"id": 891, "thing": "AnyThing4"} ]

我想在不使用循环的情况下实现这一目标。有人可以在Typescript中以任何快捷方式帮助我吗?

1 个答案:

答案 0 :(得分:0)

您正在寻找concat method

let C = A.concat(B);

(可能有重复的内容,但是找到它要比写上面的代码花更长的时间。)