Skip to content

Instantly share code, notes, and snippets.

@ervinne13
Last active October 23, 2018 02:25
Show Gist options
  • Save ervinne13/4625aa5d7903f663a442c799b784eb77 to your computer and use it in GitHub Desktop.
Save ervinne13/4625aa5d7903f663a442c799b784eb77 to your computer and use it in GitHub Desktop.
Mapping answers to questions
let questions = [{
"form_criteria_id": "46",
"field_name": "pumpstation_general",
"field_label": "General repairs required?",
"mandatory": false,
"action": "dropdown",
"input_type": [{
"id": 7,
"text": "Yes",
"value": "yes",
"created_at": "2018-10-21 22:36:18",
"updated_at": "2018-10-21 22:36:18"
},
{
"id": 8,
"text": "No",
"value": "no",
"created_at": "2018-10-21 22:36:18",
"updated_at": "2018-10-21 22:36:18"
}
],
"value": ""
},
{
"form_criteria_id": "60",
"field_name": "pumpstation_control_panel_photo",
"field_label": "Control panel photos",
"mandatory": false,
"action": "upload",
"input_type": [
],
"value": ""
}
];
let answers = [{
"id": "1",
"form_id": 1,
"form_criteria_id": 46,
"form_criteria_option_id": 7,
"value": "yes",
"text": "Yes"
}];
// Avoid n * m by creating a map first. This reduces complexity to n + m instead.
let answersCriteriaMap = [];
answers.forEach(answer => {
answersCriteriaMap[answer.form_criteria_id] = answer;
});
questions.forEach(question => {
if ((matchingAnswer = answersCriteriaMap[question.form_criteria_id])) {
return question.value = matchingAnswer.value;
}
});
console.log(questions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment