我有两个这样的数组:
let event = [
{id: "123", ename: "abc"}
{id: "125", ename: "def"}
]
let speaker = [
{id: "11", sname: "s1", event_id: "123"}, //FK from event table
{id: "13", sname: "s2", event_id: "123"},
{id: "15", name: "s4", event_id: "125"}
]
我想要这样的输出:
let event_speaker = [
{
id: "123",
ename: "abc",
speaker: [
{id: "11", sname: "s1", event_id: "123"},
{id: "13", sname: "s2", event_id: "123"},
]
},
{
id: "125",
ename: "def",
speaker: [
{id: "15", sname: "s4", event_id: "125"}
]
}
]
我尝试了不同的答案,这些答案可以将两个数组合并为一个,但当我们有多个匹配对并且我需要用我自己的密钥(扬声器)将其放入一个新数组时,则不行。
我已经尝试过这个:
let arr3 = arr1.map((item, i) => Object.assign({}, item, arr2[i]));
但它只适用于一个扬声器,因为它将所有扬声器键放入最终数组中,如下所示
let final_array = [
id: "123",
ename: "abc,
sname: "s1",
event_id: "123"
]
噜噜哒
慕田峪9158850
相关分类