Created
October 9, 2017 10:04
-
-
Save bernardo-cs/adb38f8deb70082cbc2bd24f33d2f0fb to your computer and use it in GitHub Desktop.
how to remove ifs from js (angular.js) and use filters/reduce
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var ids = []; | |
if ($scope.attendeesToggle) { ids = ids.concat($scope.eventAttendees); } | |
if ($scope.waitingListToggle) { ids = ids.concat($scope.eventWaitingList); } | |
//-------- | |
[ | |
{ condition: () => $scope.attendeesToggle, array: $scope.eventAttendees }, | |
{ condition: () => $scope.waitingListToggle, array: $scope.eventWaitingList } | |
] | |
.filter(({condition}) => condition() ) | |
.reduce((res, {array}) => res.concat(array), []) | |
//--------- | |
$scope.attendees = { | |
toggle: true, | |
users: [] | |
} | |
$scope.waitingList = { | |
toggle: true, | |
users: [] | |
} | |
['attendees', 'waitingList'].filter((x) => x.toggle ).reduce((res, acum) => res.concat(acum), []) | |
//--------- | |
$scope.attendeesToggle | |
$scope.eventAttendees | |
$scope.waitingListToggle | |
$scope.eventWaitingList | |
[ | |
{ | |
toggle: 'attendeesToggle', event: 'eventAttendees' | |
}, | |
{ | |
toggle: 'waitingListToggle', event: 'eventWaitingList' | |
}, | |
].filter((obj) => $scope[obj.toggle]) | |
.reduce((res, e) => $scope[obj.event], []) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment