如何从 JSON 中的月份列表中选择当前月份?
Posted
技术标签:
【中文标题】如何从 JSON 中的月份列表中选择当前月份?【英文标题】:How to select current month from list of month in JSON in react native? 【发布时间】:2021-12-27 06:40:45 【问题描述】:我有一个 JSON 格式的月份列表,想从 react native 的列表中选择当前月份
这是我的清单
Month: [
value: "1", label: "JAN", key: "JAN" ,
value: "2", label: "FEB", key: "FEB" ,
value: "3", label: "MAR", key: "MAR" ,
value: "4", label: "APRIL", key: "APRIL" ,
value: "5", label: "MAY", key: "MAY" ,
value: "6", label: "JUN", key: "JUN" ,
value: "7", label: "JUL", key: "JUL" ,
value: "8", label: "AUG", key: "AUG" ,
value: "9", label: "SEP", key: "SEP" ,
value: "10", label: "OCT", key: "OCT" ,
value: "11", label: "NOV", key: "NOV" ,
value: "12", label: "DEC", key: "DEC"
]
【问题讨论】:
您应该添加您自己尝试过的内容。查看:***.com/help/how-to-ask 了解如何提出好问题。 【参考方案1】:这样做会为您提供数组中的当前月份:
let Month = [
value: "1", label: "JAN", key: "JAN" ,
value: "2", label: "FEB", key: "FEB" ,
value: "3", label: "MAR", key: "MAR" ,
value: "4", label: "APRIL", key: "APRIL" ,
value: "5", label: "MAY", key: "MAY" ,
value: "6", label: "JUN", key: "JUN" ,
value: "7", label: "JUL", key: "JUL" ,
value: "8", label: "AUG", key: "AUG" ,
value: "9", label: "SEP", key: "SEP" ,
value: "10", label: "OCT", key: "OCT" ,
value: "11", label: "NOV", key: "NOV" ,
value: "12", label: "DEC", key: "DEC"
];
let currentMonth = Month[new Date().getMonth()];
console.log(currentMonth);
【讨论】:
【参考方案2】:您可以使用
找到当前月份索引const currentMonthIndex = new Date().getMonth() + 1
月份通常从 0 开始,所以你必须加 1 然后就可以通过数组进行过滤了
const selectedMonth = months.find(m => m.value === currentMonthIndex)
【讨论】:
以上是关于如何从 JSON 中的月份列表中选择当前月份?的主要内容,如果未能解决你的问题,请参考以下文章
jQuery UI datepicker:如何将下拉列表中的月份名称从短名称更改为长名称?