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

113
Vistas
how to accumulate values within a map function and store in useState

I have a map function which maps an array object of users with attributes fullname and points. Is there a way I can somehow add the points of every user whilst it maps and store it in a variable. I tried several ways with useState (in react), but I kept getting re-render error.

{users.map((user, index) => { return (
<tr key={index}>
  <td>{user.fullname}</td>
  <td>{user.points}</td>
</tr>
); })}

Any help would be appreciated..

about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

I would do it in two separate steps. Collect the points

const totalPoints = users.
    map(user => user.points).
    reduce((a,b)=> a + b, 0)

then leverage it in your code.

 { totalPoints }

In the UI / Browser it isn't likley to have too much data to be able to perform this with multiple loops, and IMO it keeps the code cleaner.

about 4 years ago · Juan Pablo Isaza Denunciar

0

You should set points for every user using foreach in another method.

const Method=()=>{
const list=  users.forEach(function(user){
  user.point= 10;
    });
    setUsers(list);
    }
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