如何将arrayobject推入另一个arrayobject Typescript

时间:2018-10-22 20:18:11

标签: angular typescript

我正在尝试在打字稿中的对象数组下面进行concat

data1 = [
  {"ID":"938","Name":"C6"},
  {"ID":"939","Name":"C7"},
  {"ID":"931","Name":"C9"}
]

data2 = [
  {"ID":"438","Name":"t6"},
  {"ID":"439","Name":"t7"},
  {"ID":"431","Name":"t9"}
]

这是所需的输出:

output = [
  {"ID":"938","Name":"C6"},
  {"ID":"939","Name":"C7"},
  {"ID":"931","Name":"C9"},
  {"ID":"438","Name":"t6"},
  {"ID":"439","Name":"t7"},
  {"ID":"431","Name":"t9"}
]

2 个答案:

答案 0 :(得分:1)

Array.concat()...spread operator)应该起作用:

const data1 = [{"ID":"938","Name":"C6"},{"ID":"939","Name":"C7"},{"ID":"931","Name":"C9"}];

const data2 = [{"ID":"438","Name":"t6"},{"ID":"439","Name":"t7"},{"ID":"431","Name":"t9"}];

const output = [...data1, ...data2];

console.log(output);

答案 1 :(得分:1)

您可以这样做:

Array.prototype.push.apply(data1,data2);

,您应该将data1数组修改为输出数组。