我有两个看起来像这样的对象:
var a = [{
id: 0,
name: "Zero"
}];
var b = [{
id: 1,
name: "one",
firstName: "First"
}],
[{
id: 2,
name: "two",
firstName: "Second"
}], [{
id: 3,
name: "three",
firstName: "Third"
}];
我想在Javascript中连接这两个对象,看起来像这样:
var c = [{
id: 0,
name: "Zero"
}],
[{
id: 1,
name: "one",
firstName: "First"
}], [{
id: 2,
name: "two",
firstName: "Second"
}], [{
id: 3,
name: "three",
firstName: "Third"
}];
有一种简单的方法吗?
答案 0 :(得分:2)
虽然您发布的内容看起来无效,但您的对象位于数组中,因此您可以使用.concat
var a = [{id: 0, name: "Zero"}];
var b = [
[{id: 1, name: "one", firstName:"First"}],
[{id: 2, name: "two", firstName:"Second"}],
[{id: 3, name: "three", firstName: "Third"}]
];
var c = a.concat(b);
如果没有理由将对象放在单个数组中,那么您需要b
:
var b = [
{id: 1, name: "one", firstName:"First"},
{id: 2, name: "two", firstName:"Second"},
{id: 3, name: "three", firstName: "Third"}
];