Skip to content

Commit 8a3f0b3

Browse files
authored
Create filter.js
1 parent 01bb3d8 commit 8a3f0b3

File tree

1 file changed

+40
-0
lines changed
  • Modern Development/ECMASCript 2021/Server-side ECMAScript 2021 examples

1 file changed

+40
-0
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
// How many times have we written long for-loops just to filter data manually?
2+
// The filter() method makes it so much simpler — one clean line to get exactly what you need!
3+
// Let’s see how we used to do it vs. how clean it can be now.
4+
5+
//before
6+
(function executeRule(current, previous /*null when async*/) {
7+
8+
var incidents = [
9+
{ number: 'INC001', state: 'In Progress' },
10+
{ number: 'INC002', state: 'Resolved' },
11+
{ number: 'INC003', state: 'New' },
12+
{ number: 'INC004', state: 'In Progress' }
13+
];
14+
15+
var inProgress = [];
16+
for (var i = 0; i < incidents.length; i++) {
17+
if (incidents[i].state === 'In Progress') {
18+
inProgress.push(incidents[i]);
19+
}
20+
}
21+
22+
gs.info('In Progress Incidents: ' + JSON.stringify(inProgress));
23+
24+
})(current, previous);
25+
26+
//after
27+
(function executeRule(current, previous /*null when async*/) {
28+
29+
var incidents = [
30+
{ number: 'INC001', state: 'In Progress' },
31+
{ number: 'INC002', state: 'Resolved' },
32+
{ number: 'INC003', state: 'New' },
33+
{ number: 'INC004', state: 'In Progress' }
34+
];
35+
36+
var inProgress = incidents.filter(inc => inc.state === 'In Progress');
37+
38+
gs.info('In Progress Incidents: ' + JSON.stringify(inProgress));
39+
40+
})(current, previous);

0 commit comments

Comments
 (0)