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

241
Vistas
map reduce count and sum of array of objects

i have an array of objects which has properties with boolean and some properties with integer values. For eg

const arr = [{
    _id: "621bb15de2ecadf024da51d3",
    draw: false,
    pixel: false,
    tooltip: 1,
    points: 1,
    
  },
  {
    _id: "621bb15de2ecadf024da51d8",
    draw: false,
    pixel: true,
    tooltip: 0,
    points: 1,
    
  },
  {
    _id: "621bb15de2ecadf024da51da",
    draw: false,
    pixel: true,
    tooltip: 1,
    points: 1,
    
  },
  {
    _id: "621bb15de2ecadf024da51e5",
    draw: false,
    pixel: true,
    tooltip: 0,
    points: 1,
    
  },
  {
    _id: "621bb15de2ecadf024da51f8",
    draw: false,
    pixel: true,
    tooltip: 1,
    points: 1,
    
  },
  {
    _id: "621bb15ee2ecadf024da5222",
    draw: false,
    pixel: true,
    tooltip: 0,
    points: 1,
    
  },
  {
    _id: "621bb15ee2ecadf024da5230",
    draw: false,
    pixel: true,
    tooltip: 1,
    points: 1,
    
  },
  {
    _id: "621bb15fe2ecadf024da52f3",
    draw: false,
    pixel: false,
    tooltip: 1,
    points: 1,
    
  },
  {
    _id: "621bb160e2ecadf024da5375",
    draw: false,
    pixel: true,
    tooltip: 0,
    points: 1,
  }
]

I am trying to achieve sum of tooltip and points, also count of draw and pixel where they are true.

const res = arr.reduce(function (
          acc,
          curr
        ) {
          return {
            tooltip: acc.tooltip + curr.tooltip,
            points:
              acc.points +
              curr.points,
          };
        });

I am getting output as below which is correct for sum,

{
  points: 9,
  tooltip: 5
}

but i also want count of draw and pixel where its value true.

Expected Result:
{
      points: 9,
      tooltip: 5,
      pixel: 7,
      draw: 0
    }
about 4 years ago · Juan Pablo Isaza
2 Respuestas
Responde la pregunta

0

Just use the same way as you did with tooltip and point, since with + operator between 2 boolean values, these boolean values will be coerced to integer

const arr = [ { _id: '621bb15de2ecadf024da51d3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51d8', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51da', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15de2ecadf024da51e5', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15de2ecadf024da51f8', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15ee2ecadf024da5222', draw: false, pixel: true, tooltip: 0, points: 1, }, { _id: '621bb15ee2ecadf024da5230', draw: false, pixel: true, tooltip: 1, points: 1, }, { _id: '621bb15fe2ecadf024da52f3', draw: false, pixel: false, tooltip: 1, points: 1, }, { _id: '621bb160e2ecadf024da5375', draw: false, pixel: true, tooltip: 0, points: 1, }, ]

const res = arr.reduce(function (acc, curr) {
  return {
    tooltip: acc.tooltip + curr.tooltip,
    points: acc.points + curr.points,
    draw: acc.draw + curr.draw,
    pixel: acc.pixel + curr.pixel,
  }
})

console.log(res)

References

12.8.3 The Addition Operator ( + )

about 4 years ago · Juan Pablo Isaza Denunciar

0

You can try this:

let countPixel = 0;
let countDraw = 0;
const res = arr.reduce(function (
    acc,
    curr
  ) {
    if (curr.pixel == true) {
        countPixel++;
    }  
    if (curr.draw == true) {
        countDraw++;
    }
    return {
        tooltip: acc.tooltip + curr.tooltip,
        points: acc.points + curr.points,
        pixel: countPixel,
        draw: countDraw,
    };
});
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