javascript - Array.prototype.reject(): TypeError: ....reject is not a function -
i taking cool online course functional programming in javascript. following along fine until instructor used array.prototype.reject()
, did not work me @ run time.
i use "reject" instead of loop because less code. but, browser, nodejs , express code consoles tell me reject not function
.
i researched other articles discuss promise.reject
not function, provide solutions not make sense scenario.
here example code in course:
var animals = [ { name: 'fluffykins', species: 'rabbit' }, { name: 'caro', species: 'dog' }, { name: 'hamilton', species: 'dog' }, { name: 'harold', species: 'fish' }, { name: 'ursula', species: 'cat' }, { name: 'jimmy', species: 'fish' } ]; var isdog = function(animal){ return animal.species === 'dog'; } var otheranimals = animals.reject(isdog);
the work-around for-loop:
var notdogs = animals.filter(function(animal){ return animal.species !== 'dog'; });
its output is:
> notdogs [ { name: 'fluffykins', species: 'rabbit' }, { name: 'harold', species: 'fish' }, { name: 'ursula', species: 'cat' }, { name: 'jimmy', species: 'fish' } ]
please me use array.prototype.reject().
editl
i found array.prototype.reject() @ github/array.prototype.reject)
array.prototype.reject
isn't thing unless library/custom code adds reject
method arrays.
to achieve want, should use array.prototype.filter
method are. i'm not sure think longer because can write same way:
var animals = [ { name: 'fluffykins', species: 'rabbit' }, { name: 'caro', species: 'dog' }, { name: 'jimmy', species: 'fish' } ]; function nodogs(animal) { return animal.species !== 'dog'; } var otheranimals = animals.filter(nodogs); console.log(otheranimals);
Comments
Post a Comment