Based on the structure you provided, you can handle the FormData as follows:
Object.entries(values).forEach(([key, value]) => {
if (Array.isArray(value)) {
value.forEach((item, index) => {
if (typeof item === 'object' && item !== null) {
Object.entries(item).forEach(([subKey, subValue]) => {
if (subValue instanceof File) {
formData.append(`${key}[${index}][${subKey}]`, subValue, subValue.name);
} else {
formData.append(`${key}[${index}][${subKey}]`, subValue);
}
});
} else {
formData.append(`${key}[${index}]`, item);
}
});
} else if (value instanceof Date) {
formData.append(key, value.toString());
} else if (value instanceof File) {
formData.append(key, value, value.name);
} else {
formData.append(key, value);
}
});
In this code, if the value is an array, we iterate over it. If an item in the array is an object (which is the case for galleryTour), we iterate over the properties of the object. If a property value is an instance of File, we append it to FormData with its name. If it's not a File, we append it as a normal value. If the value is not an array but an instance of File (which is the case for feauturedImage), we append it to FormData with its name.
Please note that this is a general approach and might need adjustments based on your specific use case. The extracts do not provide a specific solution for handling an array that includes a file in FormData.