Skip to content

Instantly share code, notes, and snippets.

@apolopena
Last active January 13, 2019 11:07
Show Gist options
  • Save apolopena/d2cb75459fcc815508af0ae810a74f45 to your computer and use it in GitHub Desktop.
Save apolopena/d2cb75459fcc815508af0ae810a74f45 to your computer and use it in GitHub Desktop.
var cars = [{make: "Honda", type: "truck"},{make: "Cadillac", type: "sedan"}, {make: "McLaren", type: "coup"}, {make: "Infinity", type: "coup"}];
function reject(array, iteratorFunction) {
return array.filter(iteratorFunction);
}
var noCoups = reject(cars, function(car) {
return car.type !== "coup";
});
// or numbers reusing 'filter' to reverse the effect of filter (only push items that return false)
var numbers = [10,20,30];
function reject2(array, iteratorFunction) {
return array.filter(function (item) {
return !iteratorFunction(item);
});
}
var lessThan15 = reject2(numbers, function(n) {
return n > 15;
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment