Home > Net >  How would you tackle this? using the reduce method to calculate the sum of a nested object value in
How would you tackle this? using the reduce method to calculate the sum of a nested object value in

Time:12-15

I need to find the total combined age of all pets in this array. I have tried this code but get a NaN output. how do i access age of pets to get the sum?

I tried this code but get a NaN output.

const totalAgeAllPets = data.reduce((result, pet) => {
return result   pet.pets.age},0)
console.log(totalAgeAllPets)

const data = [{
    name: "mark",
    age: 27,
    from: "Chicago",
    pets: [],
  },
  {
    name: "greg",
    age: 68,
    from: "Auburn",
    pets: [{
      type: "rock",
      name: "herbert",
      age: 126003219,
    }, ],
  },
  {
    name: "ken",
    age: 28,
    from: "Naperville",
    pets: [{
        type: "cat",
        name: "colby",
        age: 1,
      },
      {
        type: "dog",
        name: "charlie",
        age: 3,
      },
      {
        type: "dog",
        name: "william riker",
        age: 1,
      },
      {
        type: "dog",
        name: "thomas riker",
        age: 1,
      },
    ],
  },
  {
    name: "kristiana",
    age: 47,
    from: "Chicago",
    pets: [],
  },
  {
    name: "bryan",
    age: 27,
    from: "Lincoln Park Zoo",
    pets: [{
        type: "white tiger",
        name: "arthur",
        age: 3,
      },
      {
        type: "silverback gorilla",
        name: "tommy",
        age: 4,
      },
      {
        type: "guinea pig",
        name: "kirk",
        age: 1,
      },
      {
        type: "hamster",
        name: "spock",
        age: 1,
      },
      {
        type: "hamster",
        name: "sarek",
        age: 1,
      },
      {
        type: "cat",
        name: "quark",
        age: 1,
      },
      {
        type: "cat",
        name: "kira nerys",
        age: 1,
      },
    ],
  },
  {
    name: "anthony",
    age: 31,
    from: "Phoenix",
    pets: [],
  },
  {
    name: "stephanie",
    age: 27,
    from: "Chicago",
    pets: [{
      type: "dog",
      name: "midnight",
      age: 3,
    }, ],
  },
  {
    name: "david p",
    age: 40,
    from: "Lancaster",
    pets: [{
      type: "dog",
      name: "quinn",
      age: 8,
    }, ],
  },
  {
    name: "patrick",
    age: 39,
    from: "Poway",
    pets: [{
        type: "dog",
        name: "hazel",
        age: 7,
      },
      {
        type: "cat",
        name: "fredy perlman",
        age: 13,
      },
    ],
  },
  {
    name: "david a",
    age: 32,
    from: "San Francisco",
    pets: [{
      type: "monstera",
      name: "holey",
      age: 1,
    }, ],
  },
  {
    name: "timm",
    age: 32,
    from: "Gainsville",
    pets: [{
      type: "goldfish",
      name: "betsy",
      age: 4,
    }, ],
  },
  {
    name: "conrad",
    age: 101,
    from: "Minneapolis",
    pets: [],
  },
  {
    name: "nate",
    age: 14,
    from: "Boston",
    pets: [{
        type: "cat",
        name: "rosie",
        age: 0.005,
      },
      {
        type: "hermit crab",
        name: "kermit",
        age: 1, // 16 wow wtf
      },
      {
        type: "dog",
        name: "ernie",
        age: 4,
      },
      {
        type: "cat",
        name: "kendrick lamar jr",
        age: 6,
      },
      {
        type: "cat",
        name: "guinan",
        age: 589,
      },
      {
        type: "cat",
        name: "beverly",
        age: 1,
      },
    ],
  },
  {
    name: "temo",
    age: 25,
    from: "Osaka",
    pets: [{
        type: "dog",
        name: "nacho",
        age: 1,
      },
      {
        type: "dog",
        name: "belky",
        age: 1,
      },
      {
        type: "dog",
        name: "janeway",
        age: 4,
      },
      {
        type: "dog",
        name: "jean-luc",
        age: 12,
      },
      {
        type: "monkey",
        name: "shin chan",
        age: 1,
      },
    ],
  },
  {
    name: "griffin",
    age: 22,
    from: "Chicago",
    pets: [{
      type: "bearded dragon",
      name: "ferg",
      age: 3,
    }, ],
  },
  {
    name: "eman",
    age: 20,
    from: "Lake in the Hills",
    pets: [{
        type: "parakeet",
        name: "sunny",
        age: 400,
      },
      {
        type: "parrot",
        name: "vic fontaine",
        age: 46,
      },
      {
        type: "parrot",
        name: "worf",
        age: 48,
      },
      {
        type: "cockatoo",
        name: "spockatoo",
        age: 33,
      },
    ],
  },
  {
    name: "jürgen",
    age: 28,
    from: "Chicago",
    pets: [{
      type: "cat",
      name: "mija",
      age: 6,
    }, ],
  },
  {
    name: "michele",
    age: 33,
    from: "Rome",
    pets: [{
        type: "cat",
        name: "caesar", // "cheeto"
        age: 3,
      },
      {
        type: "cat",
        name: "cleopatra",
        age: 3,
      },
      {
        type: "cat",
        name: "tuvok",
        age: 10,
      },
      {
        type: "turtle",
        name: "spock",
        age: 13,
      },
    ],
  },
  {
    name: "mitchell",
    age: 19,
    from: "Iowa City",
    pets: [{
      type: "cat",
      name: "eddie",
      age: 7,
    }, ],
  },
  {
    name: "reuben",
    age: 41,
    from: "Durham",
    pets: [{
        type: "cat",
        name: "nico",
        age: 15,
      },
      {
        type: "cat",
        name: "rocky",
        age: 5,
      },
    ],
  },
  {
    name: "fatima",
    age: 24,
    from: "Milwaukee",
    pets: [{
      type: "snake",
      name: "reginald",
      age: 12,
    }, ],
  },
];
const totalAgeAllPets = data.reduce((result, pet) => {
  return result   pet.pets.age;
}, 0);
console.log(totalAgeAllPets);

can someone point me in the right direction?

CodePudding user response:

Each object in data array has also nested array pets, So you have to count sum of nested array and add it in global count as:

const totalAgeAllPets = data.reduce((result, pet) => {
  return result   pet.pets.reduce((a, p) => a   p.age, 0);
}, 0);
console.log(totalAgeAllPets);

const data = [{
    name: "mark",
    age: 27,
    from: "Chicago",
    pets: [],
  },
  {
    name: "greg",
    age: 68,
    from: "Auburn",
    pets: [{
      type: "rock",
      name: "herbert",
      age: 126003219,
    }, ],
  },
  {
    name: "ken",
    age: 28,
    from: "Naperville",
    pets: [{
        type: "cat",
        name: "colby",
        age: 1,
      },
      {
        type: "dog",
        name: "charlie",
        age: 3,
      },
      {
        type: "dog",
        name: "william riker",
        age: 1,
      },
      {
        type: "dog",
        name: "thomas riker",
        age: 1,
      },
    ],
  },
  {
    name: "kristiana",
    age: 47,
    from: "Chicago",
    pets: [],
  },
  {
    name: "bryan",
    age: 27,
    from: "Lincoln Park Zoo",
    pets: [{
        type: "white tiger",
        name: "arthur",
        age: 3,
      },
      {
        type: "silverback gorilla",
        name: "tommy",
        age: 4,
      },
      {
        type: "guinea pig",
        name: "kirk",
        age: 1,
      },
      {
        type: "hamster",
        name: "spock",
        age: 1,
      },
      {
        type: "hamster",
        name: "sarek",
        age: 1,
      },
      {
        type: "cat",
        name: "quark",
        age: 1,
      },
      {
        type: "cat",
        name: "kira nerys",
        age: 1,
      },
    ],
  },
  {
    name: "anthony",
    age: 31,
    from: "Phoenix",
    pets: [],
  },
  {
    name: "stephanie",
    age: 27,
    from: "Chicago",
    pets: [{
      type: "dog",
      name: "midnight",
      age: 3,
    }, ],
  },
  {
    name: "david p",
    age: 40,
    from: "Lancaster",
    pets: [{
      type: "dog",
      name: "quinn",
      age: 8,
    }, ],
  },
  {
    name: "patrick",
    age: 39,
    from: "Poway",
    pets: [{
        type: "dog",
        name: "hazel",
        age: 7,
      },
      {
        type: "cat",
        name: "fredy perlman",
        age: 13,
      },
    ],
  },
  {
    name: "david a",
    age: 32,
    from: "San Francisco",
    pets: [{
      type: "monstera",
      name: "holey",
      age: 1,
    }, ],
  },
  {
    name: "timm",
    age: 32,
    from: "Gainsville",
    pets: [{
      type: "goldfish",
      name: "betsy",
      age: 4,
    }, ],
  },
  {
    name: "conrad",
    age: 101,
    from: "Minneapolis",
    pets: [],
  },
  {
    name: "nate",
    age: 14,
    from: "Boston",
    pets: [{
        type: "cat",
        name: "rosie",
        age: 0.005,
      },
      {
        type: "hermit crab",
        name: "kermit",
        age: 1, // 16 wow wtf
      },
      {
        type: "dog",
        name: "ernie",
        age: 4,
      },
      {
        type: "cat",
        name: "kendrick lamar jr",
        age: 6,
      },
      {
        type: "cat",
        name: "guinan",
        age: 589,
      },
      {
        type: "cat",
        name: "beverly",
        age: 1,
      },
    ],
  },
  {
    name: "temo",
    age: 25,
    from: "Osaka",
    pets: [{
        type: "dog",
        name: "nacho",
        age: 1,
      },
      {
        type: "dog",
        name: "belky",
        age: 1,
      },
      {
        type: "dog",
        name: "janeway",
        age: 4,
      },
      {
        type: "dog",
        name: "jean-luc",
        age: 12,
      },
      {
        type: "monkey",
        name: "shin chan",
        age: 1,
      },
    ],
  },
  {
    name: "griffin",
    age: 22,
    from: "Chicago",
    pets: [{
      type: "bearded dragon",
      name: "ferg",
      age: 3,
    }, ],
  },
  {
    name: "eman",
    age: 20,
    from: "Lake in the Hills",
    pets: [{
        type: "parakeet",
        name: "sunny",
        age: 400,
      },
      {
        type: "parrot",
        name: "vic fontaine",
        age: 46,
      },
      {
        type: "parrot",
        name: "worf",
        age: 48,
      },
      {
        type: "cockatoo",
        name: "spockatoo",
        age: 33,
      },
    ],
  },
  {
    name: "jürgen",
    age: 28,
    from: "Chicago",
    pets: [{
      type: "cat",
      name: "mija",
      age: 6,
    }, ],
  },
  {
    name: "michele",
    age: 33,
    from: "Rome",
    pets: [{
        type: "cat",
        name: "caesar", // "cheeto"
        age: 3,
      },
      {
        type: "cat",
        name: "cleopatra",
        age: 3,
      },
      {
        type: "cat",
        name: "tuvok",
        age: 10,
      },
      {
        type: "turtle",
        name: "spock",
        age: 13,
      },
    ],
  },
  {
    name: "mitchell",
    age: 19,
    from: "Iowa City",
    pets: [{
      type: "cat",
      name: "eddie",
      age: 7,
    }, ],
  },
  {
    name: "reuben",
    age: 41,
    from: "Durham",
    pets: [{
        type: "cat",
        name: "nico",
        age: 15,
      },
      {
        type: "cat",
        name: "rocky",
        age: 5,
      },
    ],
  },
  {
    name: "fatima",
    age: 24,
    from: "Milwaukee",
    pets: [{
      type: "snake",
      name: "reginald",
      age: 12,
    }, ],
  },
];

const totalAgeAllPets = data.reduce((result, pet) => {
  return result   pet.pets.reduce((a, p) => a   p.age, 0);
}, 0);
console.log(totalAgeAllPets);

CodePudding user response:

One approach can be as shown below:

const data = [
  {
    "name": "mark",
    "age": 27,
    "from": "Chicago",
    "pets": []
  },
  {
    "name": "greg",
    "age": 68,
    "from": "Auburn",
    "pets": [
      {
        "type": "rock",
        "name": "herbert",
        "age": 126003219
      }
    ]
  },
  {
    "name": "ken",
    "age": 28,
    "from": "Naperville",
    "pets": [
      {
        "type": "cat",
        "name": "colby",
        "age": 1
      },
      {
        "type": "dog",
        "name": "charlie",
        "age": 3
      },
      {
        "type": "dog",
        "name": "william riker",
        "age": 1
      },
      {
        "type": "dog",
        "name": "thomas riker",
        "age": 1
      }
    ]
  },
  {
    "name": "kristiana",
    "age": 47,
    "from": "Chicago",
    "pets": []
  },
  {
    "name": "bryan",
    "age": 27,
    "from": "Lincoln Park Zoo",
    "pets": [
      {
        "type": "white tiger",
        "name": "arthur",
        "age": 3
      },
      {
        "type": "silverback gorilla",
        "name": "tommy",
        "age": 4
      },
      {
        "type": "guinea pig",
        "name": "kirk",
        "age": 1
      },
      {
        "type": "hamster",
        "name": "spock",
        "age": 1
      },
      {
        "type": "hamster",
        "name": "sarek",
        "age": 1
      },
      {
        "type": "cat",
        "name": "quark",
        "age": 1
      },
      {
        "type": "cat",
        "name": "kira nerys",
        "age": 1
      }
    ]
  },
  {
    "name": "anthony",
    "age": 31,
    "from": "Phoenix",
    "pets": []
  },
  {
    "name": "stephanie",
    "age": 27,
    "from": "Chicago",
    "pets": [
      {
        "type": "dog",
        "name": "midnight",
        "age": 3
      }
    ]
  },
  {
    "name": "david p",
    "age": 40,
    "from": "Lancaster",
    "pets": [
      {
        "type": "dog",
        "name": "quinn",
        "age": 8
      }
    ]
  },
  {
    "name": "patrick",
    "age": 39,
    "from": "Poway",
    "pets": [
      {
        "type": "dog",
        "name": "hazel",
        "age": 7
      },
      {
        "type": "cat",
        "name": "fredy perlman",
        "age": 13
      }
    ]
  },
  {
    "name": "david a",
    "age": 32,
    "from": "San Francisco",
    "pets": [
      {
        "type": "monstera",
        "name": "holey",
        "age": 1
      }
    ]
  },
  {
    "name": "timm",
    "age": 32,
    "from": "Gainsville",
    "pets": [
      {
        "type": "goldfish",
        "name": "betsy",
        "age": 4
      }
    ]
  },
  {
    "name": "conrad",
    "age": 101,
    "from": "Minneapolis",
    "pets": []
  },
  {
    "name": "nate",
    "age": 14,
    "from": "Boston",
    "pets": [
      {
        "type": "cat",
        "name": "rosie",
        "age": 0.005
      },
      {
        "type": "hermit crab",
        "name": "kermit",
        "age": 1
      },
      {
        "type": "dog",
        "name": "ernie",
        "age": 4
      },
      {
        "type": "cat",
        "name": "kendrick lamar jr",
        "age": 6
      },
      {
        "type": "cat",
        "name": "guinan",
        "age": 589
      },
      {
        "type": "cat",
        "name": "beverly",
        "age": 1
      }
    ]
  },
  {
    "name": "temo",
    "age": 25,
    "from": "Osaka",
    "pets": [
      {
        "type": "dog",
        "name": "nacho",
        "age": 1
      },
      {
        "type": "dog",
        "name": "belky",
        "age": 1
      },
      {
        "type": "dog",
        "name": "janeway",
        "age": 4
      },
      {
        "type": "dog",
        "name": "jean-luc",
        "age": 12
      },
      {
        "type": "monkey",
        "name": "shin chan",
        "age": 1
      }
    ]
  },
  {
    "name": "griffin",
    "age": 22,
    "from": "Chicago",
    "pets": [
      {
        "type": "bearded dragon",
        "name": "ferg",
        "age": 3
      }
    ]
  },
  {
    "name": "eman",
    "age": 20,
    "from": "Lake in the Hills",
    "pets": [
      {
        "type": "parakeet",
        "name": "sunny",
        "age": 400
      },
      {
        "type": "parrot",
        "name": "vic fontaine",
        "age": 46
      },
      {
        "type": "parrot",
        "name": "worf",
        "age": 48
      },
      {
        "type": "cockatoo",
        "name": "spockatoo",
        "age": 33
      }
    ]
  },
  {
    "name": "jürgen",
    "age": 28,
    "from": "Chicago",
    "pets": [
      {
        "type": "cat",
        "name": "mija",
        "age": 6
      }
    ]
  },
  {
    "name": "michele",
    "age": 33,
    "from": "Rome",
    "pets": [
      {
        "type": "cat",
        "name": "caesar",
        "age": 3
      },
      {
        "type": "cat",
        "name": "cleopatra",
        "age": 3
      },
      {
        "type": "cat",
        "name": "tuvok",
        "age": 10
      },
      {
        "type": "turtle",
        "name": "spock",
        "age": 13
      }
    ]
  },
  {
    "name": "mitchell",
    "age": 19,
    "from": "Iowa City",
    "pets": [
      {
        "type": "cat",
        "name": "eddie",
        "age": 7
      }
    ]
  },
  {
    "name": "reuben",
    "age": 41,
    "from": "Durham",
    "pets": [
      {
        "type": "cat",
        "name": "nico",
        "age": 15
      },
      {
        "type": "cat",
        "name": "rocky",
        "age": 5
      }
    ]
  },
  {
    "name": "fatima",
    "age": 24,
    "from": "Milwaukee",
    "pets": [
      {
        "type": "snake",
        "name": "reginald",
        "age": 12
      }
    ]
  }
];

const totalAgeOfAllPets = data.reduce((acc, curr) => {
    curr?.pets?.forEach(({age}) => {acc = acc   age});
    return acc;
}, 0);

console.log(`Total age of all pets : ${totalAgeOfAllPets}`);

CodePudding user response:

Use .flatMap() to filter out all non-owners:

data.flatMap(obj => obj.pets.length > 0 ? obj.pets : [])

You'll get an array of pet objects. Next use reduce to get the sum of all pet.age

.reduce((sum, pet) => sum   pet.age, 0);

const data =[{name:"mark",age:27,from:"Chicago",pets:[]},{name:"greg",age:68,from:"Auburn",pets:[{type:"rock",name:"herbert",age:126003219}]},{name:"ken",age:28,from:"Naperville",pets:[{type:"cat",name:"colby",age:1},{type:"dog",name:"charlie",age:3},{type:"dog",name:"william riker",age:1},{type:"dog",name:"thomas riker",age:1}],},{name:"kristiana",age:47,from:"Chicago",pets:[]},{name:"bryan",age:27,from:"Lincoln Park Zoo",pets:[{type:"white tiger",name:"arthur",age:3},{type:"silverback gorilla",name:"tommy",age:4},{type:"guinea pig",name:"kirk",age:1},{type:"hamster",name:"spock",age:1},{type:"hamster",name:"sarek",age:1},{type:"cat",name:"quark",age:1},{type:"cat",name:"kira nerys",age:1}]},{name:"anthony",age:31,from:"Phoenix",pets:[]},{name:"stephanie",age:27,from:"Chicago",pets:[{type:"dog",name:"midnight",age:3}]},{name:"david p",age:40,from:"Lancaster",pets:[{type:"dog",name:"quinn",age:8}]},{name:"patrick",age:39,from:"Poway",pets:[{type:"dog",name:"hazel",age:7},{type:"cat",name:"fredy perlman",age:13}]},{name:"david a",age:32,from:"San Francisco",pets:[{type:"monstera",name:"holey",age:1}]},{name:"timm",age:32,from:"Gainsville",pets:[{type:"goldfish",name:"betsy",age:4}]},{name:"conrad",age:101,from:"Minneapolis",pets:[]},{name:"nate",age:14,from:"Boston",pets:[{type:"cat",name:"rosie",age:0.005},{type:"hermit crab",name:"kermit",age:1},{type:"dog",name:"ernie",age:4},{type:"cat",name:"kendrick lamar jr",age:6},{type:"cat",name:"guinan",age:589},{type:"cat",name:"beverly",age:1}]},{name:"temo",age:25,from:"Osaka",pets:[{type:"dog",name:"nacho",age:1},{type:"dog",name:"belky",age:1},{type:"dog",name:"janeway",age:4},{type:"dog",name:"jean-luc",age:12},{type:"monkey",name:"shin chan",age:1}]},{name:"griffin",age:22,from:"Chicago",pets:[{type:"bearded dragon",name:"ferg",age:3}]},{name:"eman",age:20,from:"Lake in the Hills",pets:[{type:"parakeet",name:"sunny",age:400},{type:"parrot",name:"vic fontaine",age:46},{type:"parrot",name:"worf",age:48},{type:"cockatoo",name:"spockatoo",age:33}]},{name:"jürgen",age:28,from:"Chicago",pets:[{type:"cat",name:"mija",age:6}]},{name:"michele",age:33,from:"Rome",pets:[{type:"cat",name:"caesar",age:3},{type:"cat",name:"cleopatra",age:3},{type:"cat",name:"tuvok",age:10},{type:"turtle",name:"spock",age:13}]},{name:"mitchell",age:19,from:"Iowa City",pets:[{type:"cat",name:"eddie",age:7},]},{name:"reuben",age:41,from:"Durham",pets:[{type:"cat",name:"nico",age:15},{type:"cat",name:"rocky",age:5}]},{name:"fatima",age:24,from:"Milwaukee",pets:[{type:"snake",name:"reginald",age:12}]}];

const totalAgeAllPets = data.flatMap(obj => obj.pets.length > 0 ? obj.pets : []).reduce((sum, pet) => sum   pet.age, 0);

console.log(totalAgeAllPets);

  • Related