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

181
Views
how to turn a daily data object into a monthly object

so i have a question how to turn this object :

let data = [
{"branch": "london", "date": "2020-05-07", "sales": 500},
{"branch": "london", "date": "2020-05-08", "sales": 1500},
{"branch": "london", "date": "2020-05-09", "sales": 1000},
{"branch": "london", "date": "2020-06-09", "sales": 1000},
{"branch": "wales", "date": "2020-05-10", "sales": 2000},
{"branch": "wales", "date": "2020-05-11", "sales": 3000},
{"branch": "wales", "date": "2020-06-12", "sales": 2500},
{"branch": "wales", "date": "2020-08-12", "sales": 1500}
]

into that object :

let monthlyData = [
{"branch": "london", "date": "2020-05", "sales": 3000},
{"branch": "london", "date": "2020-06", "sales": 1000},
{"branch": "wales", "date": "2020-05", "sales": 5000},
{"branch": "wales", "date": "2020-06", "sales": 2500},
{"branch": "wales", "date": "2020-08", "sales": 1500},
]

so my goal here is to have every branch give me a monthly record instead of daily how to solve this as I have only managed to do it but with the date and sales expanding the current object instead of creating a new one with the new month data.

about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

You could do it with Array.prototype.reduce():

const data = [
  {"branch": "london", "date": "2020-05-07", "sales": 500},
  {"branch": "london", "date": "2020-05-08", "sales": 1500},
  {"branch": "london", "date": "2020-05-09", "sales": 1000},
  {"branch": "london", "date": "2020-06-09", "sales": 1000},
  {"branch": "wales", "date": "2020-05-10", "sales": 2000},
  {"branch": "wales", "date": "2020-05-11", "sales": 3000},
  {"branch": "wales", "date": "2020-06-12", "sales": 2500},
  {"branch": "wales", "date": "2020-08-12", "sales": 1500}
];

const monthlyData=Object.values(data.reduce((a,c)=>{ 
 const bd=c.branch+c.date.substr(0,7);             // generate a unique key for branch and month
 if (!a[bd]) a[bd]={...c,date:c.date.substr(0,7)}; // create an entry for bd
 else a[bd].sales+=c.sales;                        // add sales to existing bd entry
 return a;                                         // Object.values: create an array by
},{}));                                            // extracting all values from the object

console.log(monthlyData)

Updated:

At first I didn't spot the branch property. This has now been fixed.

about 4 years ago · Juan Pablo Isaza Report

0

i solved the issue by using 2 for each loops

let data = [
    {"branch": "london", "date": "2020-05-07", "sales": 500},
    {"branch": "london", "date": "2020-05-08", "sales": 1500},
    {"branch": "london", "date": "2020-05-09", "sales": 1000},
    {"branch": "london", "date": "2020-06-09", "sales": 1000},
    {"branch": "wales", "date": "2020-05-10", "sales": 2000},
    {"branch": "wales", "date": "2020-05-11", "sales": 3000},
    {"branch": "wales", "date": "2020-05-12", "sales": 2100},
    {"branch": "wales", "date": "2020-06-12", "sales": 2500},
    {"branch": "wales", "date": "2020-08-12", "sales": 1500},
    {"branch": "wales", "date": "2020-09-12", "sales": 200},
]
monthlyData = []

data.forEach((dataSet) => {
    if (monthlyData.length === 0) {
        newObj = {
            "branch": dataSet.branch,
            "date": dataSet.date.substring(0,7),
            "sales": dataSet.sales
        }
        monthlyData.push(newObj)
    } else {
        let found = false
        monthlyData.forEach((month) => {
            if (month.branch === dataSet.branch && month.date === dataSet.date.substring(0,7)) {
                month.sales += dataSet.sales
                found = true
            }
        })
        if (!found) {
            newObj = {
                "branch": dataSet.branch,
                "date": dataSet.date.substring(0,7),
                "sales": dataSet.sales
            }
            monthlyData.push(newObj)
        }
    }
})

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!