将数据从一个对象附加到另一个javascript

时间:2017-12-13 22:21:18

标签: javascript arrays json sorting javascript-objects

大家好我有两个对象

问题

[
  0:{
   question:"favourite food", id:"1", question_type:"text",
   sub_questions: {
     0:{
        question:"what is it's origin", parent_id:"1",
        sub_question_id:"1" question_type:"text",
     },
     1:{
        question:"how much does it cost", parent_id:"1",
        sub_question_id:"2" question_type:"text",
     }
   }
 },
 1:{
    question:"Dream car", id:"2", question_type:"text"
   },
 2:{
    question:"favourite pet", id:"2", question_type:"text"
   }
]

答案

[
  0:{
      question_id:1, response: "Fufu", sub_question: false
    },
  1:{
      parent_id:1, question_id:1, response: "Fufu originated from West Africa", sub_question: true
    },
  2:{
      parent_id:1, question_id:2, response: "USD3", sub_question: true
    },
 3:{
      question_id:3, response: "Tesla Model S", sub_question: false
    }
 4:{
      question_id:3, response: "Cat (Nameless)", sub_question: false
   }

]

我希望能够将答案附加到正确的问题字段(包括子问题)

欢迎所有建议,并提前感谢您的帮助,请注意我需要纯javascript的答案,没有jquery

1 个答案:

答案 0 :(得分:0)

看起来你将不得不遍历问题数组和附加的sub_questions数组,使用array.prototype.forEach函数,然后使用array.prototype.find函数为每个项找到并附加匹配的答案。

请找到此链接中实施的解决方案: https://jsfiddle.net/jawLtz03/

var questions = [{
    question: "favourite food",
    id: "1",
    question_type: "text",
    sub_questions: [{
        question: "what is it's origin",
        parent_id: "1",
        sub_question_id: "1",
        question_type: "text",
      },
      {
        question: "how much does it cost",
        parent_id: "1",
        sub_question_id: "2",
        question_type: "text",
      }
    ]
  },
  {
    question: "Dream car",
    id: "3",
    question_type: "text"
  },
  {
    question: "favourite pet",
    id: "4",
    question_type: "text"
  }
];

var answers = [{
    question_id: 1,
    response: "Fufu",
    sub_question: false
  },
  {
    parent_id: 1,
    question_id: 1,
    response: "Fufu originated from West Africa",
    sub_question: true
  },
  {
    parent_id: 1,
    question_id: 2,
    response: "USD3",
    sub_question: true
  },
  {
    question_id: 3,
    response: "Tesla Model S",
    sub_question: false
  },
  {
    question_id: 4,
    response: "Cat (Nameless)",
    sub_question: false
  }
];

var innerHtml = "<ul>"
questions.forEach(function(question, index) {
  innerHtml += `<li>Question ${index + 1}. ${question.question}?<br/>`;

  const hasSub = question.sub_questions ? true : false;
  question.answer = matchWithAnswer(question, false);
  innerHtml += `Answer: ${question.answer.response}.`;

  if (hasSub) {
    innerHtml += "<p><ul>";

    question.sub_questions.forEach(function(sub_question, sub_index) {
      innerHtml += `<li>Sub-Question ${sub_index + 1}. ${sub_question.question}?<br/>`;

      sub_question.answer = matchWithAnswer(sub_question, true);

      innerHtml += `Answer: ${sub_question.answer.response}.</li>`;
    });

    innerHtml += "</ul></p>";
  }

  innerHtml += "</li>";
});

function matchWithAnswer(question, isSub = false) {
  return answers.find(function(answer) {
    if (isSub) {
      return answer.sub_question == true && answer.parent_id == question.parent_id && answer.question_id == question.sub_question_id;
    } else {
      return answer.question_id == question.id;
    }
  });
}

innerHtml += "</ul>";

var div = document.getElementById('output');
div.innerHTML = innerHtml;
<h1>Q & A</h1>
<div id="output"></div>