使用连字符javascript格式化每个月5年的数据

时间:2019-06-02 11:07:38

标签: javascript

我有这种数据

[  {
    "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"
      ]
    }
  }
]

起初,我试图用年份值格式化该数据,结果就是这样。

[
  {
    "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
  }
]

这是当用户仅选择一年(例如2016年)时,那么将生成上述结果。现在,我正在尝试编制诸如以下数据。

{
    "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 ...
  }

在5年的12个月中,大约会有60列。

这是我用来制作以第二种模式显示的数据的方法

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;
}

2 个答案:

答案 0 :(得分:0)

要包含所有年份,您可以像这样运行其他循环:

{{1}}

答案 1 :(得分:0)

使用Object.entries将帮助我们更轻松地映射值:

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:
  // e.g. [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:
    // e.g. [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)