Map JSON data to multidimentional array

Viewed 52

I have the following JSON Object:

{
   "id":123456789,
   "currency":"EUR",
   "dates":{
      "2022-01-01":{
         "dayWeek":"Friday",
         "hours":{
            "14:00":{
               "hourNumber":200,
               "hourCount":236
            },
            "15:00":{
               "hourNumber":25700,
               "hourCount":569
            }
         }
      },
      "2022-01-02":{
         "dayWeek":"Monday",
         "hours":{
            "14:00":{
               "hourNumber":200,
               "hourCount":236
            },
            "15:00":{
               "hourNumber":789,
               "hourCount":569
            }
         }
      },
      "2022-01-03":{
         "dayWeek":"Sunday",
         "hours":{
            "14:00":{
               "hourNumber":478,
               "hourCount":236
            },
            "15:00":{
               "hourNumber":7895,
               "hourCount":26
            }
         }
      }
   }
}

and I want to map it to a matrix(X, Y,Value). Initialy the default data for the matrix is 0:

data: [
   [0,0,0],
   [0,1,0],
   [0,2,0],
   [0,3,0],
   [0,4,0],
   [0,5,0],
   [0,6,0],

   [1,0,0],
   [1,1,0],
   [1,2,0],
   [1,3,0],
   [1,4,0],
   [1,5,0],
   [1,6,0],

   [2,0,0],
   [2,1,0],
   [2,2,0],
   [2,3,0],
   [2,4,0],
   [2,5,0],
   [2,6,0],

   [3,0,0],
   [3,1,0],
   [3,2,0],
   [3,3,0],
   [3,4,0],
   [3,5,0],
   [3,6,0],

   [4,0,0],
   [4,1,0],
   [4,2,0],
   [4,3,0],
   [4,4,0],
   [4,5,0],
   [4,6,0],

   [5,0,0],
   [5,1,0],
   [5,2,0],
   [5,3,0],
   [5,4,0],
   [5,5,0],
   [5,6,0],

   [6,0,0],
   [6,1,0],
   [6,2,0],
   [6,3,0],
   [6,4,0],
   [6,5,0],
   [6,6,0],

   [7,0,0],
   [7,1,0],
   [7,2,0],
   [7,3,0],
   [7,4,0],
   [7,5,0],
   [7,6,0],
]

I've created a typescript model:

export interface RootObject {
  id: Number;
  currency: String;
  dates: Map<String, Hours>
}

export interface Hours {
  hours: Map<String, DataPerHour>;
}

export interface DataPerHour {
  hourNumber: Number,
  hourCount: Number
}

and I'm tring to go via nested for's through the JSON data and add it to the relevant position(I have a static Y axis that represent a day of the week(Monday->Sunday)), but I'm not sure how to start doint it exactly :

Object.entries(this.jsonData.dates).forEach((day) => {
   // day manipulation; 
   Object.entries(day).forEach((hour) => {
      // hour manipulation;
   })
})
finalDataMatrix: [
   [dayWeek[0], hours[0], hourNumber],
   [dayWeek[0], hours[1], hourNumber],
   [dayWeek[0], hours[2], hourNumber],
   [dayWeek[1], hours[0], hourNumber],
   [dayWeek[2], hours[0], hourNumber],
   [dayWeek[2], hours[1], hourNumber],
   [dayWeek[2], hours[2], hourNumber],
]
0 Answers
Related