I am trying to filter all the elements in an array which are bigger than 10 to a new array. I am intentionally not using Array.prototype.filter()
since I want to learn the reduce()
method. Here is the code I was playing with
var collection = [3, 5, 11, 23, 1];
// fileter all the elements bigger than 10 to a new array
var output = collection.reduce(function(filteredArr, collectionElemet) {
if (collectionElemet > 10) {
return filteredArr.push(collectionElemet);
}
}, []);
I was expecting that filteredArr
would be initialized with an empty array at the time of first callback execution as it happens with many examples provided here. But when I run this code, I get the error
Cannot read property 'push' of undefined
, where am I messing it up? Thank you!