/**
* Maps fields of arrays for scheduled runs with verbose logging.
* @param {Array<Object>} scheduledRuns - An array of scheduled run objects.
* @param {Object} fieldMap - An object mapping source fields to destination fields.
* @param {string} outputFieldName - The name of the field to which the mapped data will be assigned.
* @returns {Array<Object>} - The mapped array of scheduled run objects.
*/
function mapScheduledRuns(scheduledRuns, fieldMap, outputFieldName) {
if (!Array.isArray(scheduledRuns)) {
console.error("Error: scheduledRuns must be an array.");
return [];
}
if (typeof fieldMap !== 'object' || fieldMap === null) {
console.error("Error: fieldMap must be an object.");
return scheduledRuns;
}
if (typeof outputFieldName !== 'string' || outputFieldName.trim() === '') {
console.error("Error: outputFieldName must be a non-empty string.");
return scheduledRuns;
}
const mappedRuns = scheduledRuns.map(run => {
const mappedRun = {}; // Create a new object for the mapped run
for (const sourceField in run) {
if (run.hasOwnProperty(sourceField)) {
const destinationField = fieldMap[sourceField];
if (destinationField) {
mappedRun[destinationField] = run[sourceField];
} else {
console.warn(`Warning: Field '${sourceField}' not found in fieldMap. Skipping.`);
}
}
}
mappedRun[outputFieldName] = mappedRun; // Assign the mapped run to the output field
return mappedRun;
});
console.debug(`Successfully mapped ${mappedRuns.length} scheduled runs.`);
return mappedRuns;
}
Add your comment