zhanhongtao / blog

Blog for 91885076(QQ群)
http://github.com/zhanhongtao/blog/issues
24 stars 7 forks source link

按特定格式输出 #272

Closed zhanhongtao closed 7 years ago

zhanhongtao commented 7 years ago

需求如下图: 6

function log (start, end) {
  console.log(`${end.id !== start.id ? `${start.id}-${end.id}` : `${start.id}`}: ${start.name}`)
}

function handler (list) {
  if (list.length === 0) return
  let start = list[0]
  let end = start
  for (let i = 1; i <= list.length; ++i) {
    let item = list[i]
    if (
      item &&
      item.id === end.id + 1 &&
      item.name === end.name
    ) {
      end = item
    } else {
      log(start, end)
      start = end = item
    }
  }
}