数组 javascript 按 id 匹配答案和问题

Array javascript to match answer and questions by id

我有一个像这样的数组形式

const options = [
  {
    id: 1,
    content: 'Hello'
  },
  {
    id: 2,
    content: 'Hi'
  }
]

const answers = [
  {
    id: 400,
    content: 'World',
    optionKey: 1
  },
  {
    id: 500,
    content: 'There',
    optionKey: 2
  }
]

预期输出

const expecetedOutput = [
  {
    option: {
      id: 1,
      content: 'Hello'
    },
    answer: {
      id: 400,
      content: 'World'
    }
  },
  {
    option: {
      id: 2,
      content: 'Hi'
    },
    answer: {
      id: 500,
      content: 'There'
    }
  }
];

我试过的

我尝试过使用地图和查找,但结果仍然不如预期

const optionWithAnswer = answers.map((answer) => {
  return {
    option: options.find((option) => option.id === answer.optionKey),
    answer: answers.find((answer) => answer.optionKey === options.find((option) => option.id === answer.optionKey).id)
  }
})

我得到了结果。答案总是会找到答案数组的第一个索引

[
  {
    option: { id: 1, content: 'Hello' },
    answer: { id: 400, content: 'World', optionKey: 1 }
  },
  {
    option: { id: 2, content: 'Hi' },
    answer: { id: 400, content: 'World', optionKey: 1 }
  }
]

我应该做什么。这是应该使用 reduce 完成的事情吗?

options.map(opt => ({
    option: opt,
    answer: answers.find(i => i.optionKey === opt.id)
}));