如何在JavaScript中将数组和对象中的数据合并并放入一个数组中?

huangapple go评论101阅读模式
英文:

How to combine data from an array and object and put together into an array in JavaScript?

问题

arr = ["Q1A1", "Q2A3"]

假设Q1是问题1,A1是从问题中选择的选项。以下是对应Q1的对象,其中包含有关答案的详细信息,并且随着我们转到Q2,此对象也会更改。

{
id: "Q1",
answers: [
{
id: "Q1A1",
text: "是"
},
{
id: "Q1A2",
text: "否"
}
]
}

同样,如果我们在Q1中选择答案,对于Q2,我们有另一个对象。

{
id: "Q2",
answers: [
{
id: "Q2A1",
text: "测试1"
},
{
id: "Q2A2",
text: "测试2"
},
{
id: "Q2A3",
text: "测试3"
},
{
id: "Q2A4",
text: "测试4"
}
]
}

我需要通过包含问题和答案(例如,“Q1A1”)的数组查找对象,并找到所选答案的文本,即("是"),如果您查看上述问题1的对象。因此,我需要按以下方式放入数组中。

result = ["Q1_是", "Q2_测试3"]

英文:

I have an array in which I have some string value holding the id along with answer selected and I have different object in which I have the detail about answer which is selected. The below array keep updated whenever we select an option from question.

  1. arr = ["Q1A1", "Q2A3"]

assume Q1 is Question no. 1 and A1 is option selected from the question. And below I have an object for the corresponding Q1 which contained the detail about answers and this object also get change as we move over to Q2

  1. {
  2. id: "Q1",
  3. answers: [
  4. {
  5. id: "Q1A1",
  6. text: "Yes"
  7. },
  8. {
  9. id: "Q1A2",
  10. text: "No"
  11. }
  12. ]
  13. }

same way I have any another object for Q2 if we select the answer in Q1, now we have different object for Q2

  1. {
  2. id: "Q2",
  3. answers: [
  4. {
  5. id: "Q2A1",
  6. text: "Test 1"
  7. },
  8. {
  9. id: "Q2A2",
  10. text: "Test 2"
  11. },
  12. {
  13. id: "Q2A3",
  14. text: "Test 3"
  15. },
  16. {
  17. id: "Q2A4",
  18. text: "Test 4"
  19. }
  20. ]
  21. }

I need to lookup the object with the help of array which contain question and answer(eg, "Q1A1") with selected and need to find the text for answer selected i.e ("Yes") if u look into the above object for question 1. Hence I need put into the array like this way.

  1. result = ["Q1_Yes","Q2_Test3"]

答案1

得分: 1

这段代码将帮助您获取这些结果。

  1. let selected = ["Q1A1", "Q2A3"];
  2. let QA = [
  3. {
  4. id: "Q1",
  5. answers: [
  6. {
  7. id: "Q1A1",
  8. text: "Yes"
  9. },
  10. {
  11. id: "Q1A2",
  12. text: "No"
  13. }
  14. ]
  15. },
  16. {
  17. id: "Q2",
  18. answers: [
  19. {
  20. id: "Q2A1",
  21. text: "Test 1"
  22. },
  23. {
  24. id: "Q2A2",
  25. text: "Test 2"
  26. },
  27. {
  28. id: "Q2A3",
  29. text: "Test 3"
  30. },
  31. {
  32. id: "Q2A4",
  33. text: "Test 4"
  34. }
  35. ]
  36. }
  37. ];
  38. let all_answers = QA.reduce((allanswers, qa) => (qa.answers.map(d => allanswers[d.id] = [qa.id, d.text]), allanswers), {});
  39. const result = selected.map(selected => all_answers[selected].join('_'))
  40. console.log(result)

请注意,我已经保留了代码的原始格式。

英文:

This code will help you to get those results.
<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-js -->

  1. let selected = [&quot;Q1A1&quot;, &quot;Q2A3&quot;];
  2. let QA = [
  3. {
  4. id: &quot;Q1&quot;,
  5. answers: [
  6. {
  7. id: &quot;Q1A1&quot;,
  8. text: &quot;Yes&quot;
  9. },
  10. {
  11. id: &quot;Q1A2&quot;,
  12. text: &quot;No&quot;
  13. }
  14. ]
  15. },
  16. {
  17. id: &quot;Q2&quot;,
  18. answers: [
  19. {
  20. id: &quot;Q2A1&quot;,
  21. text: &quot;Test 1&quot;
  22. },
  23. {
  24. id: &quot;Q2A2&quot;,
  25. text: &quot;Test 2&quot;
  26. },
  27. {
  28. id: &quot;Q2A3&quot;,
  29. text: &quot;Test 3&quot;
  30. },
  31. {
  32. id: &quot;Q2A4&quot;,
  33. text: &quot;Test 4&quot;
  34. }
  35. ]
  36. }
  37. ];
  38. let all_answers = QA.reduce((allanswers,qa)=&gt;(qa.answers.map(d=&gt; allanswers[d.id]=[qa.id,d.text]),allanswers),{});
  39. const result = selected.map(selected =&gt; all_answers[selected].join(&#39;_&#39;))
  40. console.log(result)

<!-- end snippet -->

huangapple
  • 本文由 发表于 2023年1月6日 14:45:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75027782.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定