My array called structuredFormData
contains the following information:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644"
},
{
"season": "christmas",
"firstContactPersonName": "Tommy",
"firstContactPersonPhone": "46442644",
"secondContactPersonPhone": "Thomas"
}
]
In addition, I have an array named selectedDate
, which holds the dates:
["autumn=2020-08-28", "christmas=2020-12-28"]
To update my data for backend communication, I am using a function that creates or updates another array named updatedStructuredFormData
. This function maps through the structuredFormData
and assigns relevant dates from the selectedDate
array.
let updatedStructuredFormData = structuredFormData.map(x => {
let season = x.season;
x.date = selectedDate.find(e => e.indexOf(season) > -1).split("=")[1];
return x;
});
The challenge arises when only one date is selected and the selectedDate
array looks like this: ["autumn=2020-08-28"]
. In such cases, I need to filter the values accordingly.
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644"
}
Currently, I'm struggling with filtering out only the selected values in the dates
array. Any assistance or guidance on how to achieve this would be greatly appreciated.
The desired outcome should look like:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
"date": "2020-08-28"
}
]
If the selectedDate
array also includes a date for Christmas as well
["autumn=28-08-28", "christmas=2020-12-28"]
, then the expected result would incorporate both dates:
[
{
"season": "autumn",
"firstContactPersonName": "John",
"firstContactPersonPhone": "46442644",
"secondContactPersonName": "Jhonny",
"secondContactPersonPhone": "46442644",
"date": "2020-08-28"
},
{
"season": "christmas",
"firstContactPersonName": "Tommy",
"firstContactPersonPhone": "46442644",
"date": "2020-12-28"
}
]
Thank you for your help :)