Home > Software engineering >  Javascript: Filtering a key in Json object with the elements of one array?
Javascript: Filtering a key in Json object with the elements of one array?

Time:02-27

a couple of classic disclaimers first (and a Hello everyone, because I don't know why it keeps cutting these 2 words if I put them at the beginning!):

  • Super newbie here, started learning Javascript 10 days ago;
  • I searched a lot for a solution to my problem, but after more than 2 days trying solutions, I open this post, a sort of painful surrender.

My problem: basically, from a Json which contains a lot of records in the format {"date": "MM-DD-YYYY", "temperature": integer} I am trying to build a sort of interactive script that will take all the different months in that json, and will automatically give me the average temperature for that month. So if you change the array and add 1 month of records, immediately I get also the new average for the new month.

So, starting from the Json, my steps are to make an array with all the months included in the file (not every month is included) with a map and new date... then I will transform the "javascript months" (which starts from 0... but in the Json january is not 0 but 1) to make an array of months included in "string form" where Jan = "01", Feb = "02" and so on.

After that I am lost: I have tried a lot of combination with loops, nested loops, forEach, but I can't figure out a way to say to Javascript: "for every item in this array of months, take all the relative records from the Json, and give me an Avg Temperature!"

Basically what I would like to achieve is simple: from a given Json in that format, I retrieve an array of the months, and the end results = an array of average temperatures, 1 avg temp for every month.

I'll try to post what I did until now (only the functioning parts)!

Thank you in advance!

const dates =   [
{"date": "01-24-2020", "temps": 1},
{"date": "01-31-2020", "temps": -1},
{"date": "02-01-2020", "temps": 6},
{"date": "02-02-2020", "temps": 2},
{"date": "03-03-2020", "temps": 1},
{"date": "03-04-2020", "temps": 1},
{"date": "04-06-2020", "temps": -2},
{"date": "04-08-2020", "temps": -4}]

const singleMonths = Array.from(new Set(dates.map(elem => new Date(elem.date).getMonth())))

const singleMonthString = singleMonths.map(m => `${"0" (m 1)}`)

//after this I tried A LOT of different solutions, but I can only filter manually for a singular item in my Array, and not how I would like to, automatically for all my items in my array!

const damnFilter = dates.filter(m => m.dates.substring(0,2)==result[0]) 



CodePudding user response:

I don't know if it is that you want, but here I created some of the snippet, where it gets four months and averages each month temperature:

let newDates = [];
for(let i = 1; i <= 12; i  ){
    newDates = dates.filter(date => new Date(date.date).getMonth()   1 == i)
    if(newDates.length > 0){
    let accumulator = 0;
    newDates.map(day => accumulator  = day.temps);
    console.log("Average: ", accumulator / newDates.length);
    }
}

CodePudding user response:

You can do that:

const dates = 
  [ { date: '01-24-2020', temps:  1 } 
  , { date: '01-31-2020', temps: -1 } 
  , { date: '02-01-2020', temps:  6 } 
  , { date: '02-02-2020', temps:  2 } 
  , { date: '03-03-2020', temps:  1 } 
  , { date: '03-04-2020', temps:  1 } 
  , { date: '04-06-2020', temps: -2 } 
  , { date: '04-08-2020', temps: -4 } 
  ] 

const result = dates.reduce((r,{date,temps},i,{[i 1]:next})=>
  {
  let [m,d,y] = date.split('-')
  r[m] ??= { sum:0, count:0 }
  r[m].sum  = temps
  r[m].count  
  if (!next )
    Object.keys(r).forEach(k=>r[k] = r[k].sum / r[k].count )
  return  r   
  },{})

console.log(result)
.as-console-wrapper {max-height: 100%!important;top:0 }

  • Related