Ho questo array
bidsList = [
{
supplierName:'A',
awardedCapacity:5,
switcherStatus: true
},
{
supplierName:'B',
awardedCapacity:10,
switcherStatus: true,
},
{
supplierName:'A',
awardedCapacity:5,
switcherStatus: false,
},
{
supplierName:'A',
awardedCapacity:3,
switcherStatus: true,
},
{
supplierName:'B',
awardedCapacity:5,
switcherStatus: true,
},
{
supplierName:'C',
awardedCapacity:2,
switcherStatus: false,
},
ho bisogno di avere separete array in cui durante l'iterazione attraverso questa matrice si calcola il totale di tutte le awardedCapacities dove il nome del fornitore è lo stesso
Per esempio mi hanno array in cui ho questo output
let newArr = [
{
supplierName: 'A',
totalAwarded: 13,
},
{
supplierName:'B',
totalAwarded: 15,
},
{
supplierName:'C',
totalAwarded: 2,
}
]
La soluzione è:
let newArr = [];
bidsList.reduce(function(acc, val) {
if (!acc[val.supplierName]) {
acc[val.supplierName] = { supplierName: val.supplierName, awardedCapacity: 0 };
newArr.push(acc[val.supplierName])
}
acc[val.supplierName].awardedCapacity += val.awardedCapacity;
return acc;
}, {});
console.log(newArr);
ma ora ho bisogno di controllare anche se la switcherStatus è settata a true solo se è impostata a false non devo calcolare è awardedCapacity e che non devo spingere in un array è un oggetto
quindi l'uscita dovrebbe essere
let newArr = [
{
supplierName: 'A',
totalAwarded: 8,
},
{
supplierName:'B',
totalAwarded: 15,
},
]
C
Qui È esclusa perché è switcherStatus false, e Un 8 -, perché in oggetto è stato con switcherStatus false
non riesco a trovare un modo per modificare questo ridurre il codice qui per questo scopo.