I have my data set, and I was been able to filter it by EmployeeId from mine subobject. However, I don’t want to filter those empty objects that theirs subobjects GroupedServices
does not contain any items (length is 0)
Here is my working example:
https://jsfiddle.net/sko3y1vq/6/
JavaScript
x
208
208
1
function display_message(id) {
2
let data =
3
[
4
{
5
"ServiceTypeId": 802,
6
"ServiceTypeName": "Путне исправе",
7
"GroupedServices": [
8
{
9
"Id": 5181,
10
"ServiceTypeId": 802,
11
"ServiceName": "Путне исправе",
12
"Name": "Пасош",
13
"Duration": 20,
14
"DurationForClient": 20,
15
"Order": 1,
16
"EmployeeId": 3656
17
},
18
{
19
"Id": 5182,
20
"ServiceTypeId": 802,
21
"ServiceName": "Путне исправе",
22
"Name": "Преузимање пасоша",
23
"Duration": 20,
24
"DurationForClient": 20,
25
"Order": 1,
26
"EmployeeId": 3656
27
},
28
{
29
"Id": 5227,
30
"ServiceTypeId": 802,
31
"ServiceName": "Путне исправе",
32
"Name": "Путни лист",
33
"Duration": 20,
34
"DurationForClient": 20,
35
"Order": 1,
36
"EmployeeId": 3683
37
}
38
],
39
"Order": 1
40
},
41
{
42
"ServiceTypeId": 801,
43
"ServiceTypeName": "Овере докумената",
44
"GroupedServices": [
45
{
46
"Id": 5184,
47
"ServiceTypeId": 801,
48
"ServiceName": "Овере докумената",
49
"Name": "Наследна изјава – одрицање од наследства ",
50
"Duration": 30,
51
"DurationForClient": 30,
52
"Order": 2,
53
"EmployeeId": 3656
54
},
55
{
56
"Id": 5183,
57
"ServiceTypeId": 801,
58
"ServiceName": "Овере докумената",
59
"Name": "Наследна изјава - прихватање наследства",
60
"Duration": 20,
61
"DurationForClient": 20,
62
"Order": 2,
63
"EmployeeId": 3656
64
},
65
{
66
"Id": 5186,
67
"ServiceTypeId": 801,
68
"ServiceName": "Овере докумената",
69
"Name": "Овера копије и преписа",
70
"Duration": 20,
71
"DurationForClient": 20,
72
"Order": 2,
73
"EmployeeId": 3656
74
},
75
{
76
"Id": 5185,
77
"ServiceTypeId": 801,
78
"ServiceName": "Овере докумената",
79
"Name": "Сачињавање наследне изјаве и овера ",
80
"Duration": 40,
81
"DurationForClient": 40,
82
"Order": 2,
83
"EmployeeId": 3656
84
}
85
],
86
"Order": 2
87
},
88
{
89
"ServiceTypeId": 800,
90
"ServiceTypeName": "Матичне књиге",
91
"GroupedServices": [
92
{
93
"Id": 5222,
94
"ServiceTypeId": 800,
95
"ServiceName": "Матичне књиге",
96
"Name": "Закључење брака у дипломатско-конзуларном представништву",
97
"Duration": 40,
98
"DurationForClient": 40,
99
"Order": 3,
100
"EmployeeId": 3643
101
},
102
{
103
"Id": 5225,
104
"ServiceTypeId": 800,
105
"ServiceName": "Матичне књиге",
106
"Name": "Извод из матичне књиге рођених",
107
"Duration": 20,
108
"DurationForClient": 20,
109
"Order": 3,
110
"EmployeeId": 3656
111
},
112
{
113
"Id": 5226,
114
"ServiceTypeId": 800,
115
"ServiceName": "Матичне књиге",
116
"Name": "Царинска потврда",
117
"Duration": 20,
118
"DurationForClient": 20,
119
"Order": 3,
120
"EmployeeId": 3683
121
}
122
],
123
"Order": 3
124
},
125
{
126
"ServiceTypeId": 805,
127
"ServiceTypeName": "Визе",
128
"GroupedServices": [
129
{
130
"Id": 5247,
131
"ServiceTypeId": 805,
132
"ServiceName": "Визе",
133
"Name": "Виза Д",
134
"Duration": 30,
135
"DurationForClient": 30,
136
"Order": 4,
137
"EmployeeId": 3683
138
},
139
{
140
"Id": 5254,
141
"ServiceTypeId": 805,
142
"ServiceName": "Визе",
143
"Name": "Виза Ц",
144
"Duration": 30,
145
"DurationForClient": 30,
146
"Order": 4,
147
"EmployeeId": 3683
148
}
149
],
150
"Order": 4
151
},
152
{
153
"ServiceTypeId": 804,
154
"ServiceTypeName": "Држављанство",
155
"GroupedServices": [
156
{
157
"Id": 5246,
158
"ServiceTypeId": 804,
159
"ServiceName": "Држављанство",
160
"Name": "Отпуст из држављанства",
161
"Duration": 30,
162
"DurationForClient": 30,
163
"Order": 5,
164
"EmployeeId": 3683
165
},
166
{
167
"Id": 5248,
168
"ServiceTypeId": 804,
169
"ServiceName": "Држављанство",
170
"Name": "Пријем у држављанство",
171
"Duration": 30,
172
"DurationForClient": 30,
173
"Order": 5,
174
"EmployeeId": 3683
175
}
176
],
177
"Order": 5
178
},
179
{
180
"ServiceTypeId": 803,
181
"ServiceTypeName": "Остале услуге",
182
"GroupedServices": [
183
{
184
"Id": 5217,
185
"ServiceTypeId": 803,
186
"ServiceName": "Остале услуге",
187
"Name": "Прибављање документа из србије",
188
"Duration": 20,
189
"DurationForClient": 20,
190
"Order": 6,
191
"EmployeeId": 3656
192
}
193
],
194
"Order": 6
195
}
196
]
197
198
199
200
var test = data.map(({ ServiceTypeName, GroupedServices }) => {
201
GroupedServices = GroupedServices.filter(({ EmployeeId }) => EmployeeId == id );
202
return { ServiceTypeName, GroupedServices }
203
})
204
//var a = test.filter(({ length }) => length == 1)
205
console.log(test);
206
207
208
}
JavaScript
1
1
1
<input type="button" onclick="display_message(3683);" value="click"/>
Thanks
Advertisement
Answer
- Reduce an Array to a subset Array
- push to the accumulator array only if
GroupedServices.length > 0
JavaScript
1
9
1
function display_message(id) {
2
const result = data.reduce((arr, { ServiceTypeName, GroupedServices }) => {
3
GroupedServices = GroupedServices.filter(({ EmployeeId }) => EmployeeId == id );
4
if (GroupedServices.length > 0) arr.push({ ServiceTypeName, GroupedServices });
5
return arr;
6
}, []);
7
console.log(result);
8
}
9
Here’s a jsFiddle demo
To conclude, there’s no need to iterate twice the same array, first using .map()
and than using .filter()
. That’s why .reduce()
is the best choice for such a task: creating a subset Array in one go.