I am trying to play with the Reduce function in JavaScript. What I am trying to achieve can be easily done via the filter function but I am trying to do it via Reduce.
I am trying to retrieve all the people where the age is greater or equal to 18 years and store the result in an array
var people = [
{ name: "John", age: 16 },
{ name: "Thomas", age: 20 },
{ name: "Smith", age: 18 },
{ name: "Jessy", age: 17 },
];
var arr = [];
var output = people.reduce(function(arr,ppl){
if(ppl.age >= 18)
return(arr.push(ppl));
},arr);
console.log(output);
However when I run this snippet, I get an error that says "TypeError: Cannot read property 'push' of undefined". I am not sure where do I need to define the arr (array where I need to store the output)