@rjlevy/

SQS: groupby array

Nodejs

No description

fork
loading
Files
  • index.js
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
const delayReasons = [
  {
    id: 7,
    title: '3rd Party reinstatement - not SQS job',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 36,
    title:
      'Apparatus positioned incorrectly (ie high tubes / sections)',
    category: 'Further work required before backfill'
  },
  {
    id: 22,
    title: 'Box build required',
    category: 'Further work required before backfill'
  },
  {
    id: 19,
    title: 'Broken duct(s)',
    category: 'Further work required before backfill'
  },
  {
    id: 12,
    title: 'Building Site / Site induction required',
    category: 'Site Issues'
  },
  { id: 13, title: 'Cellar repair', category: 'Site Issues' },
  {
    id: 5,
    title: 'Concrete required (finish)',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 6,
    title: 'Concrete required (wet pour)',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 29,
    title: 'Customer not happy for reinstatement to take place',
    category: 'Works postponed'
  },
  { id: 18, title: 'Damaged cables', category: 'Hazards on site' },
  {
    id: 4,
    title: 'Damaged materials - cannot re-use',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 16,
    title: 'Dangerous materials on site (ie asbestos, oil leak)',
    category: 'Hazards on site'
  },
  {
    id: 26,
    title:
      'Excavation not ready for backfill - informed pre site visit',
    category: 'Further work required before backfill'
  },
  {
    id: 28,
    title:
      'Excavation not ready for backfill - SQS not informed before attending',
    category: 'Further work required before backfill'
  },
  {
    id: 33,
    title: 'Excavation size - too small for compactor plate',
    category: 'Site Issues'
  },
  {
    id: 20,
    title: 'High joints / High cables',
    category: 'Further work required before backfill'
  },
  {
    id: 9,
    title: 'Incorrect address / location details',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 23,
    title: 'Link box lid too high',
    category: 'Further work required before backfill'
  },
  {
    id: 35,
    title: 'Missing apparatus (ie tubes, sections, covers)',
    category: 'Further work required before backfill'
  },
  { id: 8, title: 'No access', category: 'Site Issues' },
  {
    id: 32,
    title: 'No images with work order',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 30,
    title: 'Operative did not attend site',
    category: 'Works postponed'
  },
  {
    id: 31,
    title: 'Operative instructed not to attend - task rescheduled',
    category: 'Works postponed'
  },
  { id: 27, title: 'Other', category: 'Other' },
  {
    id: 10,
    title: 'Out of hours work required',
    category: 'Site Issues'
  },
  {
    id: 11,
    title: 'Red route - SQS not informed',
    category: 'Site Issues'
  },
  {
    id: 1,
    title:
      'Site details incorrect / incomplete / missing (ie measures, surface, etc)',
    category: 'Incorrect / incomplete site info'
  },
  { id: 17, title: 'Site not safe', category: 'Hazards on site' },
  {
    id: 34,
    title: 'SLG / TM incorrect or incomplete',
    category: 'Site Issues'
  },
  {
    id: 2,
    title: 'Specialist materials required',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 3,
    title: 'Specialist tools / equipment required (ie mini digger)',
    category: 'Incorrect / incomplete site info'
  },
  {
    id: 24,
    title: 'Steel plates need moving',
    category: 'Further work required before backfill'
  },
  {
    id: 21,
    title: 'Temporary joints',
    category: 'Further work required before backfill'
  },
  {
    id: 25,
    title: 'TM needs removing',
    category: 'Further work required before backfill'
  },
  { id: 14, title: 'Tree roots', category: 'Site Issues' },
  {
    id: 15,
    title: 'Utility leak (ie water, gas, etc)',
    category: 'Hazards on site'
  }
];

const temp = [
  {
    category: 'Incorrect / incomplete site info',
    reasons: [
      { id: 7,
        title: '3rd Party reinstatement - not SQS job',
        category: 'Incorrect / incomplete site info' 
      }
    ]
  },
  {
    category: 'Further work required before backfill',
    reasons: [
      { id: 11,
        title: 'Out of hours work required',
        category: 'Tree roots' 
      }
    ]
  }
];

const delayReasonsGrouped = delayReasons.reduce((acc, obj) => {
  let found = acc.find(item => item.category === obj.category);
  let foundIndex = acc.findIndex(item => item.category === obj.category);
  if (!found) {
    acc.push({
      category: obj.category,
      reasons: [ { id: obj.id, name: obj.title } ]
    });
  } else {
    acc[foundIndex].reasons.push( { id: obj.id, name: obj.title } );
  }
  return acc;
}, []);

console.log(delayReasonsGrouped[0]);




const questions = [
  { question_id: 151, question_category: "GRAB", question: "Length used (m)", actual_answer: "3", default_answer: "" },
  { question_id: 150, question_category: "GRAB", question: "Tile tape used?", actual_answer: "Yes", default_answer: "No" },
  { question_id: 152, question_category: "GRAB", question: "Cable protection plates used?", actual_answer: "No", default_answer: "No" },
  { question_id: 150, question_category: "GRAB", question: "Tile tape used?", actual_answer: "No", default_answer: "" },
  { question_id: 149, question_category: "EXCAVATION", question: "Cable used?", actual_answer: "No", default_answer: "" },
  { question_id: 145, question_category: "DEFAULT", question: "Length used?", actual_answer: "No", default_answer: "" },
  { question_id: 145, question_category: "EXCAVATION", question: "Length used?", actual_answer: "No", default_answer: "" }
];

let questionsGrouped = questions.reduce((acc, item) => {
  if (!acc[item.question_category]) {
    acc[item.question_category] = [];
  }
  acc[item.question_category].push(item);
  return acc;
}, {});

// console.log(questionsGrouped);
node v10.16.0