Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
## `filter()` Method

The `filter()` method creates a **new array** with all elements that pass the condition implemented by the provided function.

### Syntax

array.filter((element, index, array) {
// return true to keep the element, false otherwise
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// How many times have we written long for-loops just to filter data manually?
// The filter() method makes it so much simpler — one clean line to get exactly what you need!
// Let’s see how we used to do it vs. how clean it can be now.

//before
(function executeRule(current, previous /*null when async*/) {

var incidents = [
{ number: 'INC001', state: 'In Progress' },
{ number: 'INC002', state: 'Resolved' },
{ number: 'INC003', state: 'New' },
{ number: 'INC004', state: 'In Progress' }
];

var inProgress = [];
for (var i = 0; i < incidents.length; i++) {
if (incidents[i].state === 'In Progress') {
inProgress.push(incidents[i]);
}
}

gs.info('In Progress Incidents: ' + JSON.stringify(inProgress));

})(current, previous);

//after
(function executeRule(current, previous /*null when async*/) {

var incidents = [
{ number: 'INC001', state: 'In Progress' },
{ number: 'INC002', state: 'Resolved' },
{ number: 'INC003', state: 'New' },
{ number: 'INC004', state: 'In Progress' }
];

var inProgress = incidents.filter(inc => inc.state === 'In Progress');

gs.info('In Progress Incidents: ' + JSON.stringify(inProgress));

})(current, previous);
Loading