Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

135
Views
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 answers
Answer question

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 Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!