3

how to sum of the all the stationuserAmount values in the given array

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

I was tried with map and reduce on array values to find the total sum of the given array.

    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 })}))
1
  • if you want a sum for the full array, a reduce of reduce will make it. If you want a sum for each "stationuser" array inside the array, then a map of reduce is the solution Commented Feb 14, 2022 at 14:31

2 Answers 2

5

You can do reduce inside 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));

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

Comments

0

You can do it quite easily with a nested map.

      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)

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.