简体   繁体   中英

formatting data for 5 years for each month with year hyphen javascript

I have this kind of data

[  {
    "camera_name": "Bolands Mills Arup",
    "exid": "bolands-mills-arup",
    "latest_snapshot_date": "2019-05-30T07:06:55+01:00",
    "oldest_snapshot_date": "2015-12-24T23:33:23+00:00",
    "years": {
      "2015": [
        "12"
      ],
      "2016": [
        "04",
        "08",
        "09",
        "10",
        "02",
        "06",
        "03",
        "11",
        "12",
        "01",
        "07",
        "05"
      ],
      "2017": [
        "04",
        "07",
        "10",
        "09",
        "11",
        "01",
        "02",
        "03",
        "05",
        "06",
        "08",
        "11",
        "12"
      ],
      "2018": [
        "03",
        "05",
        "06",
        "10",
        "11",
        "01",
        "02",
        "08",
        "09",
        "04",
        "07",
        "11",
        "12"
      ],
      "2019": [
        "01",
        "02",
        "03",
        "04",
        "05"
      ]
    }
  },
  {
    "camera_name": "Walls Demo",
    "exid": "central-bank-fusion",
    "latest_snapshot_date": "2019-05-30T07:07:02+01:00",
    "oldest_snapshot_date": "2015-11-08T16:30:48+00:00",
    "years": {
      "2015": [
        "12",
        "11"
      ],
      "2016": [
        "02",
        "03",
        "05"
      ],
      "2017": [
        "03",
        "08",
        "10",
        "02",
        "04",
        "05",
        "06",
        "07",
        "09",
        "11",
        "01",
        "11",
        "12"
      ],
      "2018": [
        "03",
        "04",
        "07",
        "09",
        "01",
        "02",
        "08",
        "10",
        "11",
        "05",
        "06",
        "11",
        "12"
      ],
      "2019": [
        "01",
        "02",
        "03",
        "04",
        "05"
      ]
    }
  }
]

At first, I was trying to format this data with year value, and it was resulting in this.

[
  {
    "camera_name": "Bolands Mills Arup",
    "exid": "bolands-mills-arup",
    "latest_snapshot_date": "2019-05-30T07:06:55+01:00",
    "oldest_snapshot_date": "2015-12-24T23:33:23+00:00",
    "oct": 1,
    "nov": 1,
    "dec": 1,
    "jan": 1,
    "feb": 1,
    "mar": 1,
    "apr": 1,
    "may": 1,
    "jun": 1,
    "jul": 1,
    "aug": 1,
    "sep": 1
  },
  {
    "camera_name": "Walls Demo",
    "exid": "central-bank-fusion",
    "latest_snapshot_date": "2019-05-30T07:07:02+01:00",
    "oldest_snapshot_date": "2015-11-08T16:30:48+00:00",
    "oct": 0,
    "nov": 0,
    "dec": 0,
    "jan": 0,
    "feb": 1,
    "mar": 1,
    "apr": 0,
    "may": 1,
    "jun": 0,
    "jul": 0,
    "aug": 0,
    "sep": 0
  }
]

This was when a user was selecting only one year, for example, 2016. then the above results will be generated, Right now I am trying to formulate this data such as.

{
    "camera_name": "Walls Demo",
    "exid": "central-bank-fusion",
    "latest_snapshot_date": "2019-05-30T07:07:02+01:00",
    "oldest_snapshot_date": "2015-11-08T16:30:48+00:00",
    "2015-jan": 0,
    "2015-feb": 0,
    ....
    "2015-nov": 1,
    "2015-dec": 1,
    "2016-jan": 0,
    "2016-feb": 1,
    so on ...
  }

there are going to be approximately 60 columns for 5 year's 12 months.

this is the method I am using to make data which is shown in 2nd pattern

formatDataWithYear(cameras) {
  let months_chars = {
    "01":"jan", 
    "02":"feb", 
    "03":"mar",
    "04":"apr",
    "05":"may",
    "06":"jun",
    "07":"jul",
    "08":"aug",
    "09":"sep",
    "10":"oct",
    "11":"nov",
    "12":"dec"
  }
  let year = this.year;
  var obj = cameras.map(({years, ...obj}) => {
    var months = years[year]
    for(var i in months_chars) {
      months.includes(i) ? obj[months_chars[i]] = 1 : obj[months_chars[i]] = 0
    }
    return obj
  });
  return obj;
}

To include all years, you could run an additional loop like so:

  var obj = cameras.map(({years, ...obj}) => {
      for(var year in years) {
          var months = years[year];
          for(var i in months_chars) {
              months.includes(i) ? obj[year + "-" + months_chars[i]] = 1 : obj[year + "-" + months_chars[i]] = 0
          }
      }
      return obj;
  });

Using Object.entries will help us map over the values much easier:

 const cameras = [{ "camera_name": "Bolands Mills Arup", "exid": "bolands-mills-arup", "latest_snapshot_date": "2019-05-30T07:06:55+01:00", "oldest_snapshot_date": "2015-12-24T23:33:23+00:00", "years": { "2015": [ "12" ], "2016": [ "04", "08", "09", "10", "02", "06", "03", "11", "12", "01", "07", "05" ], "2017": [ "04", "07", "10", "09", "11", "01", "02", "03", "05", "06", "08", "11", "12" ], "2018": [ "03", "05", "06", "10", "11", "01", "02", "08", "09", "04", "07", "11", "12" ], "2019": [ "01", "02", "03", "04", "05" ] } }, { "camera_name": "Walls Demo", "exid": "central-bank-fusion", "latest_snapshot_date": "2019-05-30T07:07:02+01:00", "oldest_snapshot_date": "2015-11-08T16:30:48+00:00", "years": { "2015": [ "12", "11" ], "2016": [ "02", "03", "05" ], "2017": [ "03", "08", "10", "02", "04", "05", "06", "07", "09", "11", "01", "11", "12" ], "2018": [ "03", "04", "07", "09", "01", "02", "08", "10", "11", "05", "06", "11", "12" ], "2019": [ "01", "02", "03", "04", "05" ] } } ] let months_chars = { "01": "jan", "02": "feb", "03": "mar", "04": "apr", "05": "may", "06": "jun", "07": "jul", "08": "aug", "09": "sep", "10": "oct", "11": "nov", "12": "dec" } // Map over every camera object in the array, // and deconstruct the object to give us the years, and the rest of the object. var obj = cameras.map(({years, ...obj}) => { // Loop over each entry from the years object, // and deconstruct the array into a key/value pair: // eg [year, months] => 2016, [01,02,03] etc Object.entries(years).forEach(([year, months]) => { // Then loop over each entry in the months_chars object, // and deconstruct the array into another key/value pair: // eg [key, month] => 01, "jan" Object.entries(months_chars).forEach(([key, month]) => { // Check if the months value from earlier has the specified month, // and set the objects's value to 1 or 0 accordingly obj[`${year}-${month}`] = months.includes(key) ? 1 : 0 }) }) return obj }) console.log(obj) 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM