I have a function that receives 3 inputs from the user: year, month and day.
The user can only set the month if the year has been set. The day can also only be set if the month is set.
In the end, the object looks like this:
updatedFields.push({
id: book.id,
readIn: `${year}-${month}-${day}`,
});
The problem is that the user is not obliged to inform anything. So if it just informs the year and month or just the year, I’ll have something like:
"2020--" // if he only enters the year
"2021-10-" // if he enters the year and month
"2022-03-13" // if it enters year, month and day
The question is: how do I remove the excess - dashes?
>Solution :
Construct an array from the possible values, filter out those that don’t exist, then join it by the separator.
readIn: [year, month, day].filter(Boolean).join('-')