Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

148
Vistas
total sum of nested object value in array

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 })}))
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

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

about 4 years ago · Juan Pablo Isaza Denunciar

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)

about 4 years ago · Juan Pablo Isaza Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda