The following code adds checkbox values to an array when checkboxes are checked. How can I change this so that a checkbox's value is removed from the array when a checkbox is unchecked?
var departments = [];
$("input[name='department_pk[]']").change(function() {
$("input[name='department_pk[]']:checked").each(function(i){
departments[i] = $(this).val();
});
});