Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- // 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);
Add Comment
Please, Sign In to add comment