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

215
Views
Find average number from an array of objects based from a string value

I have an array of objects like this:

const test = [
{
 "id": "1"
 "date": "2022-03-11 21:00:00"
 "temp": "200",
 "desc": "light clouds"
},
{
 "id": "2"
 "date": "2022-03-11 22:00:00"
 "temp": "220",
 "desc": "light clouds"
},
{
 "id": "3"
 "date": "2022-03-11 23:00:00"
 "temp": "205",
 "desc": "rainy clouds"
},
{
 "id": "4"
 "date": "2022-03-12 21:00:00"
 "temp": "180",
 "desc": "light clouds"
},
{
 "id": "5"
 "date": "2022-03-12 22:00:00"
 "temp": "200",
 "desc": "rainy"
},
{
 "id": "6"
 "date": "2022-03-12 23:00:00"
 "temp": "200",
 "desc": "rainy"
},
]

What I want to do is to map over these objects and find the average temp for each day with the same date. And then also find the desc which appears the most times for that date too.

I am not really sure where to even begin, I am guessing I will have to use a map() function and then .find() but unsure of the syntax.

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

0

You can do it with one reduce method:

const test = [
  { "id": "1","date": "2022-03-11 21:00:00", "temp": "200", "desc": "light clouds" },
  { "id": "2", "date": "2022-03-11 22:00:00", "temp": "220", "desc": "light clouds" },
  {  "id": "3", "date": "2022-03-11 23:00:00", "temp": "205", "desc": "rainy clouds" },
  { "id": "4", "date": "2022-03-12 21:00:00", "temp": "180","desc": "light clouds"},
  {"id": "5", "date": "2022-03-12 22:00:00", "temp": "200","desc": "rainy"},
  { "id": "6", "date": "2022-03-12 23:00:00",  "temp": "200", "desc": "rainy" },
];

const result = test.reduce((accumulator, currentItem) => {
  const day = currentItem.date.substring(0, 10);
  if(!accumulator[day]){
    accumulator[day] = {
      count: 1,
      total_temp: parseInt(currentItem.temp),
      descriptions:[{count:1, description: currentItem.desc}]
    }
  } else {
    accumulator[day].count += 1;
    accumulator[day].total_temp +=  parseInt(currentItem.temp);
    const index = accumulator[day].descriptions.findIndex((item)=> item.description === currentItem.desc);
    if(index !== -1) {
       accumulator[day].descriptions[index].count += 1;
    } else {
      accumulator[day].descriptions.push({count:1, description: currentItem.desc});
    }
  }
  accumulator[day].average_temp = accumulator[day].total_temp/accumulator[day].count;
  return accumulator;
}, {});

console.log(result)

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!