1. /**
  2. * Exports form submission data with default values for routine automation.
  3. *
  4. * @param {object} formData - The form data object.
  5. * @param {object} defaultValues - An object containing default values for each field.
  6. * @returns {object} - An object containing the processed form data with default values applied.
  7. */
  8. function exportFormData(formData, defaultValues) {
  9. const processedData = {};
  10. for (const key in formData) {
  11. if (formData.hasOwnProperty(key)) {
  12. // Apply default value if the field is empty or doesn't have a value.
  13. if (!formData[key] || formData[key].trim() === "") {
  14. processedData[key] = defaultValues[key] || ""; // Use default or empty string if default is not provided
  15. } else {
  16. processedData[key] = formData[key];
  17. }
  18. }
  19. }
  20. return processedData;
  21. }
  22. // Example Usage (not part of the function, just for demonstration)
  23. // const myFormData = {
  24. // name: "",
  25. // email: "test@example.com",
  26. // phone: null,
  27. // city: ""
  28. // };
  29. //
  30. // const myDefaultValues = {
  31. // name: "John Doe",
  32. // email: "default@example.com",
  33. // phone: "123-456-7890",
  34. // city: "Anytown"
  35. // };
  36. //
  37. // const exportedData = exportFormData(myFormData, myDefaultValues);
  38. // console.log(exportedData);

Add your comment