So I have an array of objects with weekdays, it looks like this:
const weekDays = [
{
id: 1,
name: 'monday'
},
{
id: 2,
name: 'tuesday'
},
{
id: 3,
name: 'wednesday'
},
{
id: 4,
name: 'thursday'
},
{
id: 5,
name: 'friday'
},
{
id: 6,
name: 'saturday'
},
{
id: 7,
name: 'sunday'
}
]
And I have a function for formatting a day, it is used for displaying days in select component, it looks like this:
export const formatDay = flip(nth)(['monday', 'tuesday', 'wednesday', 'thursday', 'friday', 'saturday', 'sunday'])
So this function should return a day name by its id, for example if we pass 1 it should return monday etc., though currently it returns days by their index, so if we pass 1 it returns tuesday, though flip should’ve dealt with that problem
2
Answers
flip
function in your code is not handling the zero-based index of the array properly. Thenth
function expects a zero-based index, but you are passing theid
which starts from1
. You will need to adjust the input to thenth
function by subtracting 1 from the input value.Alternative way:
Leveraging Composition principles: