Welcome to my first time asking a question.
I am currently working on integrating data from two different API endpoints served by a Django Rest Framework backend and displaying it using VueJS on the frontend.
The main issue I'm encountering is how to combine sections and questions from a questionnaire with their corresponding answers. The questionnaire information comes from one endpoint, while the answers come from another. Here's a snippet of the data:
Data for Sections & Questions
{
"survey_id": 2,
"survey_name": "My Survey",
"instructions": "Instructions.",
"other_header_info": ""
"section": [
{
"section_id": 2,
"section_name": "Section Name",
"section_title": "Section Title",
"section_instructions": "Some Instructions",
"section_required_yn": true,
"question": [
{
"question_id": 2,
"question_name": "Question One.",
"question_subtext": "None.",
"answer_required_yn": true,
"input_type_id": {
"id": 3,
"input_type_name": "Dropdown"
},
"option_group_id": "1 - 10",
"allow_multiple_option_answers_yn": false
},
{
"section_id": 3,
"section_name": "Another Section",
"section_title": "Section Title",
"section_instructions": "Section Instructions",
"section_required_yn": true,
"question": [
{
"question_id": 10,
"question_name": "Another question to be answered",
"question_subtext": "None.",
"answer_required_yn": true,
"input_type_id": {
"id": 3,
"input_type_name": "Dropdown"
},
"option_group_id": "1 - 10",
"allow_multiple_option_answers_yn": false
},
Answers Data
"results": [
{
"id": 100,
"answer_numeric": 4,
"answer_text": null,
"answer_yn": null,
"answer_group": "4-ojepljuu",
"question_id": 2,
},
{
"id": 101,
"answer_numeric": 1,
"answer_text": null,
"answer_yn": null,
"answer_group": "4-ojepljuu",
"user_id": 4,
"question_id": 5,
},
I understand that I need to match the 'question_id' fields from both datasets. However, I'm struggling with how to achieve this.
My goal is to create a new dataset where answers are appended to the questions. Additionally, I aim to provide flexibility as I have various survey types with different numbers of sections and questions.
I want to organize the data into sections to customize the frontend views accordingly.
I've attempted to loop through sections and questions following an example I found here: Merge two array of objects based on a key, but haven't made much progress.
Since I'm still relatively new to this, any help, guidance, or even a functioning example would be highly appreciated.
Update: I've made some progress by creating a test function that updates the section/question object with dummy data.
var a = this.answers;
var s = this.section;
var newObj = { answer_text: "test1", answer_numeric: "test2" };
for (var idx3 = 0; idx3 < s.length; idx3++) {
for (var idx4 = 0; idx4 < s[idx3].question.length; idx4++) {
Object.assign(s[idx3].question[idx4], newObj);
}
}
Now, each question object within every section includes key/value pairs for 'answer_text' and 'answer_numeric'.
The next challenge is to retrieve matching answer data by comparing the 'question_id' in the answer object with the same in the question object.
Any suggestions?