2 回答
TA贡献1784条经验 获得超7个赞
您可以使用map和filter方法来做到这一点。您只需要speaker使用 中当前元素的 id 过滤数组event。
let event = [{"id":"123","ename":"abc"},{"id":"125","ename":"def"}]
let speaker = [{"id":"11","sname":"s1","event_id":"123"},{"id":"13","sname":"s2","event_id":"123"},{"id":"15","name":"s4","event_id":"125"}]
const event_speaker = event.map(({ id, ...rest }) => ({
id, ...rest, speaker: speaker.filter(({ event_id }) => event_id === id)
}))
console.log(event_speaker)
TA贡献1794条经验 获得超7个赞
您也可以使用对象破坏:
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 result = event.map(e => ({...e,speaker: speaker.filter(({event_id}) => event_id === e.id)}))
console.log(result)
添加回答
举报