I have a list of months in JSON, want to select the current month from the list in react native
Here is my list
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" }
]
CodePudding user response:
You can find the current month index using
const currentMonthIndex = new Date().getMonth() 1
Month usually start from 0 so you have to add 1 Then you can filter through the array
const selectedMonth = months.find(m => m.value === currentMonthIndex)
CodePudding user response:
By doing so it gives you the curent month from your array :
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);
<iframe name="sif1" sandbox="allow-forms allow-modals allow-scripts" frameborder="0"></iframe>