数组中嵌套对象值的总和

total sum of nested object value in array

如何计算给定数组中所有 stationuserAmount 值的总和

            "staions": [
                {
                    "name": "Guwahati",                        
                    "stationuser": [
                        {
                            "name": "Suranjan Dey",                                
                            "stationuserAmount": "190"
                        }        
                       
                    ]
                },                                     
                {
                    "name": "Bhubaneshwar",                        
                    "stationuser": [
                        {
                            "name": "Soubhagya Behera",                                
                            "stationuserAmount": "280"
                        },
                        {
                            "name": "Om Prakash Sahoo",                                
                            "stationuserAmount": "280"
                        }
                    ]
                }             
            ]

我尝试使用 map 和 reduce 数组值来查找给定数组的总和。

    1) const salesuserTotalCount = this.state.staions.map(items => ({          
    users: items.stationuser.reduce((a, b) => {return 
    a.stationuserAmount+b.stationuserAmount}    
    )}))
   2) const salesuserTotalCount = this.state.staions.map(items => ({
    ...items,
    users: items.stationuser.map(stationitems 
   =>stationitems.stationuserAmount).reduce((a, c) 
              => { return a + c })}))

你可以在reduce里面做reduce

const stations = [
    {
        "name": "Guwahati",                        
        "stationuser": [
            {
                "name": "Suranjan Dey",                                
                "stationuserAmount": 190
            }        
           
        ]
    },                                     
    {
        "name": "Bhubaneshwar",                        
        "stationuser": [
            {
                "name": "Soubhagya Behera",                                
                "stationuserAmount": 280
            },
            {
                "name": "Om Prakash Sahoo",                                
                "stationuserAmount": 280
            }
        ]
    }             
]

console.log(stations.reduce((acc, curr)=>{
    return acc+curr.stationuser.reduce((a,c)=>a+c.stationuserAmount,0)
},0));

您可以使用嵌套地图轻松完成。

      const data =      { "stations": [
                {
                    "name": "Guwahati",                        
                    "stationuser": [
                        {
                            "name": "Suranjan Dey",                                
                            "stationuserAmount": 190
                        }        
                       
                    ]
                },                                     
                {
                    "name": "Bhubaneshwar",                        
                    "stationuser": [
                        {
                            "name": "Soubhagya Behera",                                
                            "stationuserAmount": 280
                        },
                        {
                            "name": "Om Prakash Sahoo",                                
                            "stationuserAmount": 280
                        }
                    ]
                }             
            ]
      }
let sum = 0
data.stations.map(station => {
  station.stationuser.map(user => sum += user.stationuserAmount)
})
console.log(sum)