如何从数组对象中获取最早和最晚的日期

时间:2017-12-21 01:33:57

标签: javascript arrays sorting

我的代码按预期工作,但我很难在我的数据中找到并添加earliest_datenext_datelatest_date

let tasks_data = [
    {
      'id': 10376401,
      'name': 'closed',
      'notes': null,
      'start_date': '2017-12-23',
      'end_date': '2018-01-07',
      'start_time': null,
      'end_time': null,
      'color': '#f99bd0',
      'color_id': 5,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': null,
      'project': null,
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-13T00:58:16.577+00:00',
      'updated_at': '2017-11-13T00:58:16.577+00:00',
      'deleted_at': null
    },
    {
      'id': 10438883,
      'name': '',
      'notes': null,
      'start_date': '2018-02-17',
      'end_date': '2018-02-17',
      'start_time': null,
      'end_time': null,
      'color': '#ccaf53',
      'color_id': 36,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': 1501267,
      'project': {
        'id': 1501267,
        'name': 'sue',
        'color': '#ccaf53',
        'color_id': 36,
        'client': null,
        'created_at': '2017-11-17T03:14:11.459+00:00',
        'updated_at': '2017-11-17T03:14:11.459+00:00'
      },
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-17T03:15:48.055+00:00',
      'updated_at': '2017-11-17T03:15:48.055+00:00',
      'deleted_at': null
    },
    {
      'id': 10438875,
      'name': 'Sue',
      'notes': null,
      'start_date': '2018-01-27',
      'end_date': '2018-01-27',
      'start_time': null,
      'end_time': null,
      'color': '#ccaf53',
      'color_id': 36,
      'estimated_hours': 0,
      'done': false,
      'user_id': 961775,
      'project_id': 1501267,
      'project': {
        'id': 1501267,
        'name': 'sue',
        'color': '#ccaf53',
        'color_id': 36,
        'client': null,
        'created_at': '2017-11-17T03:14:11.459+00:00',
        'updated_at': '2017-11-17T03:14:11.459+00:00'
      },
      'folder_id': null,
      'weight': 0,
      'created_at': '2017-11-17T03:14:11.903+00:00',
      'updated_at': '2017-11-17T03:14:50.363+00:00',
      'deleted_at': null
    }
  ]

  var start_dates = new Array()
  var end_dates = new Array()
  var result = tasks_data.reduce(function (acc, item) {
    var task = acc.find(function (accItem) {
      return accItem.project_id == item.project_id
    })
    if (task && !Array.isArray(task.schedule)) {
      task.schedule = [task.schedule].concat({
        project_id: item.project_id,
        start_date: item.start_date,
        end_date: item.end_date,
        daily_estimate: item.estimated_hours,
      })
      task.start_dates.push(item.start_date)
      task.end_dates.push(item.end_date)
      task.next_start_dates.push(item.start_date)
    } else if (task && Array.isArray(task.schedule)) {
      task.schedule.push({
        project_id: item.project_id,
        start_date: item.start_date,
        end_date: item.end_date,
        daily_estimate: item.estimated_hours,
      })
      task.start_dates.push(item.start_date)
      task.end_dates.push(item.end_date)
      task.next_start_dates.push(item.start_date)
    } else {
      acc.push({
        project_id: item.project_id,
        schedule: {
          project_id: item.project_id,
          start_date: item.start_date,
          end_date: item.end_date,
          daily_estimate: item.estimated_hours,
        },
        start_dates: [item.start_date],
        end_dates: [item.end_date],
        next_start_dates: [item.start_date],
      })
    }

    return acc
  }, [])

  console.log(result) // I got the dates stored in an array. I will now use the final result to parse it and get the earliest, latest and next_start date. Don't know how to simplify it. 
  
  function getEarliest (array_dates) {
    return array_dates.reduce(function (pre, cur) {
      return Date.parse(pre) > Date.parse(cur) ? cur : pre
    })
  }
<body>
<h1>Hello, World!</h1>
<p>Welcome to <strong>Foo</strong>.</p>
</body>

“最早”是start_date,最新的是end_date,而next_start也是start_date,不包括查询的“最早”。

我的预期输出是:

[
{
  "project_id": null,
  "schedule": {
    "project_id": null,
    "start_date": "2017-12-23",
    "end_date": "2018-01-07",
    "daily_estimate": 0
  },
  earliest: "2017-12-23",
  latest: "2018-01-07",
  next_start: "2017-12-23"
},
  {
    "project_id": 1501267,
    "schedule": [
      {
        "project_id": 1501267,
        "start_date": "2018-02-17",
        "end_date": "2018-02-17",
        "daily_estimate": 0
      },
      {
        "project_id": 1501267,
        "start_date": "2018-01-27",
        "end_date": "2018-01-27",
        "daily_estimate": 0
      }
    ],
    earliest: "2018-01-27",
    latest: "2018-02-17",
    next_start: "2018-02-17"
  }
]

0 个答案:

没有答案