Come calcolare ogni oggetto nell'array array separato basato su truthy condizione?

0

Domanda

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.

algorithm javascript
2021-11-23 12:31:04
1

Migliore risposta

1

Basta aggiungere una condizione nella tua reduce funzione

var 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,
  }
];

var result = bidsList.reduce((a, c) => {
  if (c.switcherStatus) {
    let supplier = a.find(e => e.supplierName == c.supplierName);
    if (supplier)
      supplier.totalAwarded += c.awardedCapacity;
    else
      a.push({
        supplierName: c.supplierName,
        totalAwarded: c.awardedCapacity
      });
  }
  return a;
}, []);

console.log(result);

2021-11-23 12:56:44

In altre lingue

Questa pagina è in altre lingue

Русский
..................................................................................................................
Polski
..................................................................................................................
Română
..................................................................................................................
한국어
..................................................................................................................
हिन्दी
..................................................................................................................
Français
..................................................................................................................
Türk
..................................................................................................................
Česk
..................................................................................................................
Português
..................................................................................................................
ไทย
..................................................................................................................
中文
..................................................................................................................
Español
..................................................................................................................
Slovenský
..................................................................................................................