/**
* Aggregates configuration values from an array of configuration objects.
*
* @param {Array<object>} configurations An array of configuration objects. Each object
* is expected to have a 'name' property (string)
* and a 'value' property (number).
* @returns {object} An object where keys are configuration names and values are
* the sum of their corresponding values. Returns an empty object
* if the input array is empty or invalid.
*/
function aggregateConfigurations(configurations) {
if (!Array.isArray(configurations) || configurations.length === 0) {
return {}; // Handle empty or invalid input
}
const aggregatedValues = {};
for (const config of configurations) {
if (typeof config !== 'object' || config === null || !config.hasOwnProperty('name') || !config.hasOwnProperty('value')) {
continue; // Skip invalid configuration entries
}
const name = config.name;
const value = Number(config.value); //Convert value to number
if (isNaN(value)) {
continue; // Skip if value is not a number
}
if (aggregatedValues.hasOwnProperty(name)) {
aggregatedValues[name] += value;
} else {
aggregatedValues[name] = value;
}
}
return aggregatedValues;
}
Add your comment