Javascript 在ES6中,如何基于其他对象详细信息从一个对象获取对象详细信息

Javascript 在ES6中,如何基于其他对象详细信息从一个对象获取对象详细信息,javascript,arrays,json,reactjs,ecmascript-6,Javascript,Arrays,Json,Reactjs,Ecmascript 6,有两个文件,分别是questions.json和answers.json。目的是制作一个摘要页面,根据questionID和answerValues以及answerValues和values.id显示所选答案及其问题,它们位于answers.json中。有没有选择将结果带入一个变量 演示: 我在英语六级考试中的坏习惯 const results = answers.map(answer => { console.log(answer) return questions.find(q

有两个文件,分别是questions.json和answers.json。目的是制作一个摘要页面,根据
questionID
answerValues
以及
answerValues
values.id
显示所选答案及其问题,它们位于answers.json中。有没有选择将结果带入一个变量

演示:

我在英语六级考试中的坏习惯

const results = answers.map(answer => {
  console.log(answer)
  return questions.find(question => {
    console.log(question);
    return question.questionID == answer.questionID
  });
});
console.log(results)
Questions.JSON

const questions = [
  {
    questionID: 1,
    title: "Your gender ?",
    values: [{ id: "1", value: "Male" }, { id: "2", value: "Female" }]
  },
  {
    questionID: 2,
    title: "Choose your age ?",
    values: []
  },
  {
    questionID: 3,
    title: "Do you look at a screen within one hour of going to sleep ?",
    values: [{ id: "1", value: "Yes" }, { id: "2", value: "No" }]
  },
  {
    questionID: 4,
    title: "What do you do most time on mobile",
    values: [
      { id: "1", value: "Social media" },
      { id: "2", value: "Play game" },
      { id: "3", value: "Chat" },
      { id: "4", value: "Surf on internet" },
      { id: "5", value: "Call" }
    ]
  },
  {
    questionID: 5,
    title: "On average, how many hours of sleep do you get every night ?",
    values: [
      { id: "1", value: "4-6" },
      { id: "2", value: "7-9" },
      { id: "3", value: "10-11" }
    ]
  },
  {
    questionID: 6,
    title: "Any additional comments or information we should know ?",
    values: []
  }
];
const answers = [
  {
    questionID: "1",
    answerValues: "1"
  },
  {
    questionID: "2",
    answerValues: "12"
  },
  {
    questionID: "3",
    answerValues: "1"
  },
  {
    questionID: "4",
    answerValues: ["2", "4"]
  },
  {
    questionID: "5",
    answerValues: "2"
  },
  {
    questionID: "6",
    answerValues: "323123123123"
  }
];
Answers.JSON

const questions = [
  {
    questionID: 1,
    title: "Your gender ?",
    values: [{ id: "1", value: "Male" }, { id: "2", value: "Female" }]
  },
  {
    questionID: 2,
    title: "Choose your age ?",
    values: []
  },
  {
    questionID: 3,
    title: "Do you look at a screen within one hour of going to sleep ?",
    values: [{ id: "1", value: "Yes" }, { id: "2", value: "No" }]
  },
  {
    questionID: 4,
    title: "What do you do most time on mobile",
    values: [
      { id: "1", value: "Social media" },
      { id: "2", value: "Play game" },
      { id: "3", value: "Chat" },
      { id: "4", value: "Surf on internet" },
      { id: "5", value: "Call" }
    ]
  },
  {
    questionID: 5,
    title: "On average, how many hours of sleep do you get every night ?",
    values: [
      { id: "1", value: "4-6" },
      { id: "2", value: "7-9" },
      { id: "3", value: "10-11" }
    ]
  },
  {
    questionID: 6,
    title: "Any additional comments or information we should know ?",
    values: []
  }
];
const answers = [
  {
    questionID: "1",
    answerValues: "1"
  },
  {
    questionID: "2",
    answerValues: "12"
  },
  {
    questionID: "3",
    answerValues: "1"
  },
  {
    questionID: "4",
    answerValues: ["2", "4"]
  },
  {
    questionID: "5",
    answerValues: "2"
  },
  {
    questionID: "6",
    answerValues: "323123123123"
  }
];

您忘记返回
问题的结果。查找

const results = answers.map(answer => {
  return questions.find(question => {
    return question.questionID == answer.questionID
  })
})
如果您需要在两者之间进行某种合并,我的方法如下:

const results1 = answers.map((answer) => {
  const question = questions.find(question => question.questionID == answer.questionID)
  return { question: question.title, answer: answer.answerValues }
})
然后,您将得到一个具有以下结构的关联Q&A对象数组:

{
  "question": "Choose your age ?",
  "answer": "12"
}

然后在你的反应组件中

<h3>{result.title}</h3> Answer: {result.value}

// Results:
/// What do you do most time on mobile
/// Answer: Play game, Surf on internet
{result.title}答案:{result.value}
//结果:
///你大部分时间在手机上做什么
///回答:玩游戏,上网冲浪

首先,您需要意识到的是,您没有处理JSON。。。它们只是javascript对象。接下来要意识到的是,您需要声明
答案
问题
,并尝试使用
答案
问题
code@JaromandaX谢谢你的建议。我没有得到第二点第二点是你的变量名<代码>变量问题=
变量答案=
-但在代码中,您处理的是
问题
答案
。。。undefined@JaromandaX刚刚更新,谢谢你advice@Muhammed你可以查看我的答案和沙箱。我相信它解决了你的问题谢谢你的帮助。答案就在那里,目前它得到的是基于
questionID
的确切问题。根据
answerValues
?@Muhammed I以快速方式更新的相同方式回答的任何选项,您可以这样做,也可以根据需要更改数据结构该函数的目的是向用户显示,用户选择的答案及其作为摘要的问题您能给我一个例子说明
结果应该是什么样子吗。