如何在 Javascript 中连接两个具有不同结构的对象?
How can I concat two objects with different structure in Javascript?
我有两个看起来像这样的对象:
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"
}];
有没有简单的方法来做到这一点?
尽管您发布的内容看起来无效,但您的对象在数组中,因此您可以使用 .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"}
];
我有两个看起来像这样的对象:
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"
}];
有没有简单的方法来做到这一点?
尽管您发布的内容看起来无效,但您的对象在数组中,因此您可以使用 .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"}
];