/**
* Maps fields of files for routine automation.
*
* @param {Array<Object>} files An array of file objects. Each file object should have a 'fields' property,
* which is an array of field names.
* @param {Object} mapping A mapping object where keys are field names from the files, and values are the
* corresponding target field names.
* @returns {Array<Object>} An array of mapped file objects. Returns an empty array if input is invalid.
*/
function mapFields(files, mapping) {
if (!Array.isArray(files) || typeof mapping !== 'object' || mapping === null) {
console.error("Invalid input: files must be an array and mapping must be an object.");
return [];
}
const mappedFiles = [];
for (const file of files) {
if (typeof file !== 'object' || file === null || !Array.isArray(file.fields)) {
console.warn("Skipping invalid file:", file); //Log invalid files, but continue processing
continue;
}
const mappedFile = {};
for (const field of file.fields) {
if (mapping.hasOwnProperty(field)) {
mappedFile[mapping[field]] = file[field]; // Map field
} else {
mappedFile[field] = file[field]; // Keep original if no mapping
}
}
mappedFiles.push(mappedFile);
}
return mappedFiles;
}
Add your comment