I am taking a cool online course about functional programming in JavaScript. I was following along just fine until the instructor used the Array.prototype.reject()
and it did not work for me at run time.
I would like to use "reject" instead of a for loop because it is less code. But, my browser, NodeJS and Express Code consoles tell me that reject is not a function
.
I researched other articles that discuss promise.reject
is not a function, but provide solutions that do not make sense to my scenario.
Here is the example code in the 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 with a 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 help me use Array.prototype.reject().
EditL
I found Array.prototype.reject() at GitHub/Array.prototype.reject)
reduce() method in JavaScript is used to reduce the array to a single value and executes a provided function for each value of the array (from left-to-right) and the return value of the function is stored in an accumulator. Syntax: array.reduce( function(total, currentValue, currentIndex, arr), initialValue )
One can use filter() function in JavaScript to filter the object array based on attributes. The filter() function will return a new array containing all the array elements that pass the given condition. If no elements pass the condition it returns an empty array.
You can reject a promise with any value, not just an error object. const p = Promise. reject(42); return p.
The filter() method creates a new array filled with elements that pass a test provided by a function. The filter() method does not execute the function for empty elements. The filter() method does not change the original array.
Array.prototype.reject
isn't a thing unless a library/custom code adds the reject
method to Arrays.
To achieve what you want, you should use the Array.prototype.filter
method like you already are. I'm not sure what you think is longer about it because you can write it the 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);
As mentioned above, this was mentioned in a youtube series about functional programming. The video uploader did correct this mistake with an annotation, but you may not have seen that if you were viewing the video on a mobile device.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With