1. /**
  2. * Maps fields of arrays for scheduled runs with verbose logging.
  3. * @param {Array<Object>} scheduledRuns - An array of scheduled run objects.
  4. * @param {Object} fieldMap - An object mapping source fields to destination fields.
  5. * @param {string} outputFieldName - The name of the field to which the mapped data will be assigned.
  6. * @returns {Array<Object>} - The mapped array of scheduled run objects.
  7. */
  8. function mapScheduledRuns(scheduledRuns, fieldMap, outputFieldName) {
  9. if (!Array.isArray(scheduledRuns)) {
  10. console.error("Error: scheduledRuns must be an array.");
  11. return [];
  12. }
  13. if (typeof fieldMap !== 'object' || fieldMap === null) {
  14. console.error("Error: fieldMap must be an object.");
  15. return scheduledRuns;
  16. }
  17. if (typeof outputFieldName !== 'string' || outputFieldName.trim() === '') {
  18. console.error("Error: outputFieldName must be a non-empty string.");
  19. return scheduledRuns;
  20. }
  21. const mappedRuns = scheduledRuns.map(run => {
  22. const mappedRun = {}; // Create a new object for the mapped run
  23. for (const sourceField in run) {
  24. if (run.hasOwnProperty(sourceField)) {
  25. const destinationField = fieldMap[sourceField];
  26. if (destinationField) {
  27. mappedRun[destinationField] = run[sourceField];
  28. } else {
  29. console.warn(`Warning: Field '${sourceField}' not found in fieldMap. Skipping.`);
  30. }
  31. }
  32. }
  33. mappedRun[outputFieldName] = mappedRun; // Assign the mapped run to the output field
  34. return mappedRun;
  35. });
  36. console.debug(`Successfully mapped ${mappedRuns.length} scheduled runs.`);
  37. return mappedRuns;
  38. }

Add your comment