你会如何解决这个问题?使用reduce方法计算此数组中嵌套对象值的总和?

Posted

技术标签:

【中文标题】你会如何解决这个问题?使用reduce方法计算此数组中嵌套对象值的总和?【英文标题】:How would you tackle this? using the reduce method to calculate the sum of a nested object value in this array? 【发布时间】:2022-01-18 08:03:48 【问题描述】:

我需要找出这个数组中所有宠物的总年龄。我已经尝试过这段代码,但得到了一个 NaN 输出。我如何访问宠物的年龄以获得总和?

我尝试了这段代码,但得到了一个 NaN 输出。

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);

有人能指出正确的方向吗?

【问题讨论】:

【参考方案1】:

data 数组中的每个对象也有嵌套数组pets,因此您必须计算嵌套数组的总和并将其添加到全局计数中:

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);

【讨论】:

【参考方案2】:

一种方法可以如下所示:

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`);

【讨论】:

curr?.pets?.forEach(( age ) => acc += age); 更干净。虽然两者都是一样的...... 这是什么:curr?.pets? 是不是像["curr?"]["pets?"]?还是"?" 有什么特别之处? @zer00ne "?"用于防止应用程序崩溃。在这种情况下,它将处理 curr 不是对象且 pets 不是数组或 curr 中不存在 pets 的情况。【参考方案3】:

使用.flatMap() 过滤掉所有非所有者:

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

您将获得一组pet 对象。接下来使用reduce得到所有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);

【讨论】:

【参考方案4】:

另一种,只有一条线的解决方案:

const age = data.flatMap((o) => o.pets).reduce((acc, o) => acc + o.age, 0);
console.log(age) //126004497.005

【讨论】:

以上是关于你会如何解决这个问题?使用reduce方法计算此数组中嵌套对象值的总和?的主要内容,如果未能解决你的问题,请参考以下文章

hive如何调整map数和reduce数

reduce函数原理

operator模块和functools模块

如何使用 reduce() 计算合并值?

数据倾斜解决方法

使用reduce的方法实现对象数组去重