Similar to this data format, how to cut into two arrays, divided into morning time, afternoon time, with 12: 00 as the dividing line

similar to this kind of data, how to cut into two arrays, divided into morning time and afternoon time, with 12: 00 as the dividing line
[{time: 8:30}, {time: 8:15}], [{time: 9: 00}, {time: 9: 10}, {time: 9: 15}], [{time: 10: 00}], [{time: 1:00}], [{time: 1:15}]

May.27,2021

instead of using date to calculate the time, it is better to use a regular expression to match the number of hours. If it is greater than 12, it is afternoon, and less than 12, it is morning

.
/\s*(\d{1,2})\s*:\s*\d{1,2}\*/

match the first grouping


const data = [[{time: '8:30'}, {time: '8:15'}],[{time: '9: 00'}, {time: '12: 10'}, {time: '9: 15'}],[{time: '10: 00'}],[{time: '14:00'}], [{time: '1:15'}]]


function trans (data) {
  data = [].concat.apply([], data) // 
  const before = []
  const after = []
  data.forEach(cur => {
    let hour = +cur.time.split(':')[0].trim()
    if (hour < 12) {
      before.push(Object.assign({}, cur))
    } else {
      after.push(Object.assign({}, cur))
    }
  })
  return {
    before,
    after
  }
}
trans(data)
Menu