|
| 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