Reputation: 51
This is the data in json file. the _id i am passing is month number.
Const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
I am trying to convert the month number to month name by using this function. but its not working.
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income, monthNames) => ({ ...income, name: monthNames[income._id.getMonth()] }
)
), [incomes, monthNames]);
Upvotes: 1
Views: 1020
Reputation: 105
Try this!
I think when you passed the monthNames in the .map it declared it as a new variable and that's why the result was undefined.
P.S. Added -1 to income._id so the months would match
Hope it helps :)
const incomes = [
{
"_id": 6,
"total": 3000
},
{
"_id": 7,
"total": 3500
}
]
const monthNames = ["January", "Feburary", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"]
const data = useMemo(
() => incomes.map((income) => ({ ...income, name: monthNames[income._id -1]}
)
), [incomes, monthNames]);
Upvotes: 3