0

I'm trying to learn array method chaining and am using a basic example - https://jsbin.com/surizinifi/edit?js,console

const data = [
  {
    name: 'Butters',
    age: 3,
    type: 'dog'
  },
  {
    name: 'Lizzy',
    age: 6,
    type: 'dog'
  },
  {
    name: 'Red',
    age: 1,
    type: 'cat'
  },
  {
    name: 'Joey',
    age: 3,
    type: 'dog'
  },
];


const ages = data
  .filter(animal => animal.type === 'dog')
  .map(animal => animal.age * 7)
  .reduce((sum, animal) => sum + animal.age)

console.log('ages', ages);

I'm getting "NaN" returned as a posed to a numeric output (e.g. 84). Any ideas what I'm doing wrong here?

2
  • In your reduce you don't gen an animal object but animalAge number - the map from the previous step gives you an array of numbers. Commented Sep 30, 2019 at 8:40
  • 1
    You could have debugged this problem very easily. Commented Sep 30, 2019 at 8:40

3 Answers 3

1

By the mapping, you get an array of numbers and then you need no property for summing.

A good idea is to use a start value for the accumulator and prevent to use both first values of the array in the first loop.

const data = [
  {
    name: 'Butters',
    age: 3,
    type: 'dog'
  },
  {
    name: 'Lizzy',
    age: 6,
    type: 'dog'
  },
  {
    name: 'Red',
    age: 1,
    type: 'cat'
  },
  {
    name: 'Joey',
    age: 3,
    type: 'dog'
  },
];


const ages = data
  .filter(animal => animal.type === 'dog')
  .map(animal => animal.age * 7)
  .reduce((sum, age) => sum + age, 0)

console.log('ages', ages);

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

Comments

1

The method .map() returns the array of the number, there is no property to sum

.reduce((sum, age) => sum + age)

const data = [
  {
    name: 'Butters',
    age: 3,
    type: 'dog'
  },
  {
    name: 'Lizzy',
    age: 6,
    type: 'dog'
  },
  {
    name: 'Red',
    age: 1,
    type: 'cat'
  },
  {
    name: 'Joey',
    age: 3,
    type: 'dog'
  },
];


const ages = data
  .filter(animal => animal.type === 'dog')
  .map(animal => animal.age * 7)
  .reduce((sum, age) => sum + age, 0)

console.log('ages', ages);

Comments

0

You could also pass on a new array of animals to the reduce-method where each animal's age was multiplied by 7:

const data = [
  {
    name: 'Butters',
    age: 3,
    type: 'dog'
  },
  {
    name: 'Lizzy',
    age: 6,
    type: 'dog'
  },
  {
    name: 'Red',
    age: 1,
    type: 'cat'
  },
  {
    name: 'Joey',
    age: 3,
    type: 'dog'
  },
];


const ages = data
  .filter(animal => animal.type === 'dog')
  .map(animal => ({
    ...animal,
    age: animal.age * 7
   }))
  .reduce((sum, animal) => sum + animal.age, 0)

console.log('ages', ages);

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.