Array.Prototype.Reject

Opposite of Array.Prototype.Filter()

by LyndseyB

JavaScript

Array.prototype.reject = function(callback) {
    var output = [];
    for(var i = 0; i<this.length; i++) {
        if(!callback(this[i], i)) {
            output.push(this[i]);
        };
    }
    return output;
};

var items = [
    { name: "Julie", species: "Dog" },
    { name: "Jake", species: "Dog" },
    { name: "Jasper", species: "Cat" },
    { name: "Judie", species: "Dog" },
    { name: "Bob", species: "Fish" },
    { name: "Lyndsey", species: "Human" },
    { name: "Jessica", species: "Human" }    
];

var getDogs = function(item) {
    return item.species === 'Dog';   
};

var noDogs = items.reject(getDogs);
var onlyDogs = items.filter(getDogs);

console.table(noDogs);
console.table(onlyDogs);