1

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)

4
  • arr is going to be the function’s last return value. Are you always returning a value? Commented Feb 17, 2018 at 10:09
  • Array.prototype.push returns the new length of the Array. You need to push then return arr. If the age is < 18 you return undefined ... Commented Feb 17, 2018 at 10:10
  • Understood, Thank you very much Commented Feb 17, 2018 at 10:16
  • 1
    One of the beauties of reduce() is that you don't need an outer var. Just start with } []); Commented Feb 17, 2018 at 10:22

3 Answers 3

1

try this:

you should return arr,not return arr.push(ppl) because [].push(3) return 1(new length of [] ) not [3].And reduce works with accumulator which in this case the accumulator is arr.So,you should return arr not return (arr.push(ppl));

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)
    (arr.push(ppl));
  return arr;

}, arr);
console.log(output);

Sign up to request clarification or add additional context in comments.

3 Comments

Thanks a lot. This definitely works. Please help me understand whats wrong with my code. Appreciate your help
@Manish,because you return(arr.push(ppl)); you should return arr
"because [].push(1) return 1 not [1]" Because .push() returns the new length of the array (and not the pushed element)
0
 return(arr.push(ppl));

As arr.push returns the new length of the array, it will return a number. So at the next iteration arr will be a number, and you cant push to that. So you need to pass on the array:

 const output = people.reduce((arr, person) => person.age > 17 ? arr.concat(person): arr, []);

That works as arr.concat returns an array.

1 Comment

…and more importantly, in the else case nothing was returned at all
0

You can use array.concat to push to an array and return it, and put that in a ternary operator:

let people=[{name:"John",age:16},{name:"Thomas",age:20},{name:"Smith",age:18},{name:"Jessy",age:17}]

let output = people.reduce((arr,ppl) => {
    return ppl.age >= 18 ? arr.concat(ppl) : arr
},[])

console.log(output)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.