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

143
Vistas
set sum of dynamic keys within an object inside an array of object

I have one array of object which looks something like this:-

const myObjArr = [
        {
            "channelName": "AFM",
            "21-Apr-2022": 2,
            "22-Apr-2022": 2,
            "27-Apr-2022": 1,
            "29-Apr-2022": 3,
            "19-Apr-2022": 1
        },
        {
            "channelName": "Organic Others",
            "6-Apr-2022": 6,
            "27-Apr-2022": 4,
            "7-Apr-2022": 3,
            "21-Apr-2022": 1,
            "8-Apr-2022": 1
        },
        {
            "channelName": "website",
            "27-Apr-2022": 1
        }
    ]

now I want to add one more key, which is named as total in each object of this array which hold sum of all date keys to clarify I am providing the required output in the reqArray variable below

reqArray = [
        {
            "channelName": "AFM",
            "21-Apr-2022": 2,
            "22-Apr-2022": 2,
            "27-Apr-2022": 1,
            "29-Apr-2022": 3,
            "19-Apr-2022": 1,
            "total":9
        },
        {
            "channelName": "Organic Others",
            "6-Apr-2022": 6,
            "27-Apr-2022": 4,
            "7-Apr-2022": 3,
            "21-Apr-2022": 1,
            "8-Apr-2022": 1
            "total": 15
        },
        {
           "channelName": "website",
            "27-Apr-2022": 1,
            "total" : 1
        }
    ]
about 4 years ago · Juan Pablo Isaza
1 Respuestas
Responde la pregunta

0

  function addFieldsForItem (arr = []) {
    arr.forEach(function(item) {
      let total = 0;
      Object.keys(item).forEach(function(key) {
        if (typeof item[key] === 'number') {
          total = total + item[key]
        }
      })
      item.total = total
    })
    return arr;
  }
  
  

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