Last active
October 23, 2018 02:25
-
-
Save ervinne13/4625aa5d7903f663a442c799b784eb77 to your computer and use it in GitHub Desktop.
Mapping answers to questions
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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