Skip to content

Instantly share code, notes, and snippets.

@jsdecena
Last active October 23, 2018 02:29
Show Gist options
  • Save jsdecena/f8d3fea1a592c77dc45221a9b9f7e26a to your computer and use it in GitHub Desktop.
Save jsdecena/f8d3fea1a592c77dc45221a9b9f7e26a to your computer and use it in GitHub Desktop.
QNA JSON
// Questions array:
const 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":""
}
]
// Answers array:
const answers = [
{
"id":"1",
"form_id":1,
"form_criteria_id":46,
"form_criteria_option_id":7,
"value":"yes",
"text":"Yes"
}
]
// Merge into
[
{
"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":"yes" /** Set the answer here for criteria id of 46 **/
},
{
"form_criteria_id":"60",
"field_name":"pumpstation_control_panel_photo",
"field_label":"Control panel photos",
"mandatory":false,
"action":"upload",
"input_type":[
],
"value":""
}
]
// Solution?
questions.forEach(q => {
answers.forEach(a => {
if (a.form_criteria_id == q.form_criteria_id) {
return q.value = a.value;
}
});
});
// Alternate??
questions.map(q => {
answers.filter(a => {
return q.form_criteria_id == a.form_criteria_id ? q.value = a.value : '';
});
});
console.log(questions);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment