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

141
Views
Filter/Reduce array of objects into a new object based on day

Consider the following array of objects:

const data = [
   *{...more data from previous dates}*,
   {
     unixDate: 1650348034,  //yesterday
     dateTime: Tue Apr 19 2022 23:00:34,
     severityLevel: 1,
     severity: "Light"
   },
   {
     unixDate: 1650348034,  //yesterday
     dateTime: Tue Apr 19 2022 14:00:34,
     severityLevel: 3,
     severity: "Moderate"
   },
   {
     unixDate: 1650440700,  //today
     dateTime: Wed Apr 20 2022 15:45:00,
     severityLevel: 2,
     severity: "Moderate-Light"
   },
   {
     unixDate: 1650442500,  //today
     dateTime: Wed Apr 20 2022 15:45:00,
     severityLevel: 4,
     severity: "Heavy"
   },
   {
     unixDate: 1650427234,  //today
     dateTime: Wed Apr 20 2022 12:00:00,
     severityLevel: 3,
     severity: "Moderate"
   }

]

I would like to return the following:

{
   *///...records from previous dates,* 
   1650348034 : 2, //yesterday record taking only one of the unixtimestamp, and the average value of 'severityLevel'.
   1650440700 : 3  //same as above but unixtimestamp is today.
}

I'm using the dayjs package to determine whether or not the date is today, via the isToday plugin, but couldn't think of how to compare the dates. The data is growing everyday as it records new readings. I'm not too familiar with the array filter/reduce methods in ES6, would they be useful here? Any help is appreciated!

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

0

Another approach with an util method dayBegins which will calculate the day begin time stamp. This will be useful identify whether timestamps fall into same day or not.

Along with that, just build an tracking object with keys as dayBegin and values with severityLevel and number occurences.

const dayBegins = (uDate) =>
  "" + uDate * 1000 - ((uDate * 1000) % (24 * 60 * 60 * 1000));

const process = (arr, output = {}) => {
  arr.forEach(({ unixDate, severityLevel }) => {
    const key = dayBegins(unixDate);
    if (key in output) {
      output[key].count += 1;
      output[key].sum += severityLevel;
    } else {
      output[key] = {
        unixDate,
        count: 1,
        sum: severityLevel,
      };
    }
  });
  return Object.values(output).reduce(
    (acc, { unixDate, sum, count }) =>
      Object.assign(acc, { [unixDate]: sum / count }),
    {}
  );
};

const data = [
  {
    unixDate: 1650348034, //yesterday
    dateTime: "Tue Apr 19 2022 23:00:34",
    severityLevel: 1,
    severity: "Light",
  },
  {
    unixDate: 1650348034, //yesterday
    dateTime: "Tue Apr 19 2022 14:00:34",
    severityLevel: 3,
    severity: "Moderate",
  },
  {
    unixDate: 1650440700, //today
    dateTime: "Wed Apr 20 2022 15:45:00",
    severityLevel: 2,
    severity: "Moderate-Light",
  },
  {
    unixDate: 1650442500, //today
    dateTime: "Wed Apr 20 2022 15:45:00",
    severityLevel: 4,
    severity: "Heavy",
  },
  {
    unixDate: 1650427234, //today
    dateTime: "Wed Apr 20 2022 12:00:00",
    severityLevel: 3,
    severity: "Moderate",
  },
];

console.log(process(data))

about 4 years ago · Juan Pablo Isaza Report

0

First you need to convert those unix times to javascript Date objects. Then you can group by the year/month/date and then average the results.

const data = [   
   {
     unixDate: 1650348034,  //yesterday
     dateTime: "Tue Apr 19 2022 23:00:34",
     severityLevel: 1,
     severity: "Light"
   },
   {
     unixDate: 1650348034,  //yesterday
     dateTime: "Tue Apr 19 2022 14:00:34",
     severityLevel: 3,
     severity: "Moderate"
   },
   {
     unixDate: 1650440700,  //today
     dateTime: "Wed Apr 20 2022 15:45:00",
     severityLevel: 2,
     severity: "Moderate-Light"
   },
   {
     unixDate: 1650442500,  //today
     dateTime: "Wed Apr 20 2022 15:45:00",
     severityLevel: 4,
     severity: "Heavy"
   },
   {
     unixDate: 1650427234,  //today
     dateTime: "Wed Apr 20 2022 12:00:00",
     severityLevel: 3,
     severity: "Moderate"
   }

]

const x = Object.values(data.map(x => ({...x, dateTime: new Date(x.unixDate * 1000)}))
              .reduce( (acc,i) => {
                  const key = "" + i.dateTime.getFullYear() + i.dateTime.getMonth() + i.dateTime.getDate();
                  acc[key] = acc[key] || [];
                  acc[key].push(i);
                  return acc
              },{}))
              .reduce( (obj,rec) => {
                  return {...obj, [rec[0].unixDate]: rec.reduce( (acc,i) => acc+i.severityLevel,0) / rec.length }
              },{})
console.log(x)

Nothe that if the property dateTime is already a javascript date you can do without the data.map(x => ({...x, dateTime: new Date(x.unixDate * 1000)})) part.

about 4 years ago · Juan Pablo Isaza Report

0

Gets a unique list of days and builds an object based on the calculated days

const data = [ { unixDate: 1650348034, dateTime: "Tue Apr 19 2022 23:00:34", severityLevel: 1, severity: "Light" }, { unixDate: 1650348034, dateTime: "Tue Apr 19 2022 14:00:34", severityLevel: 3, severity: "Moderate" }, { unixDate: 1650440700, dateTime: "Wed Apr 20 2022 15:45:00", severityLevel: 2, severity: "Moderate-Light" }, { unixDate: 1650442500, dateTime: "Wed Apr 20 2022 15:45:00", severityLevel: 4, severity: "Heavy" }, { unixDate: 1650427234, dateTime: "Wed Apr 20 2022 12:00:00", severityLevel: 3, severity: "Moderate" } ]

//Get unique list of days
let unique = [... new Set(data.map(d => new Date(d.unixDate * 1000).toLocaleDateString("en-US")))];

//build object based on this list
let results = Object.fromEntries(unique.map(m => {
  let records = data.filter(v => new Date(v.unixDate * 1000).toLocaleDateString("en-US") === m); 
  return [records[0].unixDate, records.reduce((v,o) => v+=o.severityLevel, 0) / records.length ]
}));

console.log(results);

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!