You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何对JSON数组值按日期排序并转换为指定格式HTML表格

你可以把console.log(frequencyList)之后的原有表格渲染逻辑,全部替换为以下代码即可实现需求:

// 1. 将嵌套的日期列表展开为一维结构化数组
const flatList = frequencyList.flatMap(projectItem => {
  // 每个项目的每一条提醒日期拆分为独立条目
  return projectItem.recurrenceList.map(dateStr => {
    return {
      projectName: projectItem.project,
      reminderDate: dateStr
    }
  })
})

// 2. 按提醒日期升序排序(YYYY-MM-DD格式字符串可直接按字典序比较,和时间顺序一致)
flatList.sort((a, b) => a.reminderDate.localeCompare(b.reminderDate))

// 3. 渲染到表格
const $reminderTable = $("#tablereminder")
// 先清空原有内容
$reminderTable.find("tbody").remove()
const $newTbody = $("<tbody></tbody>")
flatList.forEach(item => {
  const tableRow = `<tr>
    <td>${item.projectName}</td>
    <td>${item.reminderDate}</td>
  </tr>`
  $newTbody.append(tableRow)
})
$reminderTable.append($newTbody)

如果运行环境不支持ES6+的flatMap方法,可以用reduce代替实现扁平化逻辑:

const flatList = frequencyList.reduce((total, projectItem) => {
  const projectDates = projectItem.recurrenceList.map(dateStr => ({
    projectName: projectItem.project,
    reminderDate: dateStr
  }))
  return total.concat(projectDates)
}, [])

内容的提问来源于stack exchange,提问作者Remesh sree

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.01 05:27:03