Guest User

Untitled

a guest
Oct 22nd, 2018
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.31 KB | None | 0 0
  1. // Questions array:
  2. const questions = [
  3. {
  4. "form_criteria_id":"46",
  5. "field_name":"pumpstation_general",
  6. "field_label":"General repairs required?",
  7. "mandatory":false,
  8. "action":"dropdown",
  9. "input_type":[
  10. {
  11. "id":7,
  12. "text":"Yes",
  13. "value":"yes",
  14. "created_at":"2018-10-21 22:36:18",
  15. "updated_at":"2018-10-21 22:36:18"
  16. },
  17. {
  18. "id":8,
  19. "text":"No",
  20. "value":"no",
  21. "created_at":"2018-10-21 22:36:18",
  22. "updated_at":"2018-10-21 22:36:18"
  23. }
  24. ],
  25. "value":""
  26. },
  27. {
  28. "form_criteria_id":"60",
  29. "field_name":"pumpstation_control_panel_photo",
  30. "field_label":"Control panel photos",
  31. "mandatory":false,
  32. "action":"upload",
  33. "input_type":[
  34.  
  35. ],
  36. "value":""
  37. }
  38. ]
  39.  
  40. // Answers array:
  41. const answers = [
  42. {
  43. "id":"1",
  44. "form_id":1,
  45. "form_criteria_id":46,
  46. "form_criteria_option_id":7,
  47. "value":"yes",
  48. "text":"Yes"
  49. }
  50. ]
  51.  
  52. // Merge into
  53. [
  54. {
  55. "form_criteria_id":"46",
  56. "field_name":"pumpstation_general",
  57. "field_label":"General repairs required?",
  58. "mandatory":false,
  59. "action":"dropdown",
  60. "input_type":[
  61. {
  62. "id":7,
  63. "text":"Yes",
  64. "value":"yes",
  65. "created_at":"2018-10-21 22:36:18",
  66. "updated_at":"2018-10-21 22:36:18"
  67. },
  68. {
  69. "id":8,
  70. "text":"No",
  71. "value":"no",
  72. "created_at":"2018-10-21 22:36:18",
  73. "updated_at":"2018-10-21 22:36:18"
  74. }
  75. ],
  76. "value":"yes" /** Set the answer here for criteria id of 46 **/
  77. },
  78. {
  79. "form_criteria_id":"60",
  80. "field_name":"pumpstation_control_panel_photo",
  81. "field_label":"Control panel photos",
  82. "mandatory":false,
  83. "action":"upload",
  84. "input_type":[
  85.  
  86. ],
  87. "value":""
  88. }
  89. ]
  90.  
  91. // Solution?
  92. questions.forEach(q => {
  93. answers.forEach(a => {
  94. if (a.form_criteria_id == q.form_criteria_id) {
  95. return q.value = a.value;
  96. }
  97. });
  98. });
  99.  
  100. // Alternate??
  101. questions.map(q => {
  102. answers.filter(a => {
  103. return q.form_criteria_id == a.form_criteria_id ? q.value = a.value : '';
  104. });
  105. });
  106. console.log(questions);
Add Comment
Please, Sign In to add comment