如何快速生成适用于cascader的数据格式?-灵析社区

复古直男

dataOptions: [ { value: "1", label: "1月", children: [ { value: "1", label: "1号" }, { value: "2", label: "2号" }, // ... { value: "31", label: "31号" }, ], }, { value: "2", label: "2月", children: [ { value: "1", label: "1号" }, { value: "2", label: "2号" }, // ... { value: "28", label: "28号" }, ], }, // .... { value: "12", label: "12月", children: [ { value: "1", label: "1号" }, { value: "2", label: "2号" }, // ... { value: "31", label: "31号" }, ], }, ],

阅读量:17

点赞量:0

问AI
封装个函数,把年份传进去就行了 const months = []; for (let i = 1; i <= 12; i++) { const month = { value: `${i}`, label: `${i}月`, children: [], }; const daysInMonth = new Date(2023, i, 0).getDate(); for (let j = 1; j <= daysInMonth; j++) { month.children.push({ value: `${j}`, label: `${j}号`, }); } months.push(month); } console.log(months);