遍历对象数组和共享 属性 值上的 group/increment 对象值

Loop through array of objects and group/increment object values on shared property value

我正在考虑循环遍历此数据对象数组的最佳方法,如果 wineRegion 名称出现不止一次,我希望输出数组使用 [= 保存此值一次14=] 值递增到所有匹配对象的值 totalPrice.

这是我的数据数组:

const data = [
  {
    wineName: 'Château Pape-Clément',
    wineRegion: 'Bordeaux (Red)',
    unitSize: '12x75cl',
    wineVintage: 2010,
    qty: 1,
    totalPrice: 1650,
  },
  {
    wineName: 'Château Beauséjour Duffau-Lagarrosse',
    wineRegion: 'Bordeaux (Red)',
    unitSize: '6x75cl',
    wineVintage: 2010,
    qty: 1,
    totalPrice: 1400,
  },
  {
    wineName: 'Vosne-Romanée, Berthaut',
    wineRegion: 'Burgundy (Red)',
    unitSize: '12x75cl',
    wineVintage: 2017,
    qty: 1,
    totalPrice: 510,
  },
  {
    wineName: 'Mazis-Chambertin, Faiveley',
    wineRegion: 'Burgundy (Red)',
    unitSize: '6x75cl',
    wineVintage: 2018,
    qty: 1,
    totalPrice: 790,
  },
  {
    wineName: 'Gevrey-Chambertin Les Champeaux, O. Bernstein',
    wineRegion: 'Burgundy (Red)',
    unitSize: '3x75cl',
    wineVintage: 2019,
    qty: 1,
    totalPrice: 675,
  },
  {
    wineName: 'Latricières-Chambertin, J. M. Fourrier',
    wineRegion: 'Burgundy (Red)',
    unitSize: '6x75cl',
    wineVintage: 2017,
    qty: 1,
    totalPrice: 1050,
  },
  {
    wineName: 'Corton Rognet, Taupenot-Merme',
    wineRegion: 'Burgundy (Red)',
    unitSize: '6x75cl',
    wineVintage: 2019,
    qty: 1,
    totalPrice: 600,
  },
  {
    wineName: 'Corton-Charlemagne, Ponsot',
    wineRegion: 'Burgundy (White)',
    unitSize: '6x75cl',
    wineVintage: 2012,
    qty: 1,
    totalPrice: 980,
  },
  {
    wineName: 'Bollinger Grand Année',
    wineRegion: 'Champagne ',
    unitSize: '6x75cl',
    wineVintage: 2008,
    qty: 1,
    totalPrice: 675,
  },
  {
    wineName: "L'Astre, David Léclapart",
    wineRegion: 'Champagne ',
    unitSize: '6x75cl',
    wineVintage: 2013,
    qty: 1,
    totalPrice: 540,
  },
  {
    wineName: 'Brunello di Montalcino Madonna delle Grazie, Marroneto',
    wineRegion: 'Italy ',
    unitSize: '6x75cl',
    wineVintage: 2012,
    qty: 1,
    totalPrice: 945,
  },

];

因此,例如,输出数组将以与上述类似的格式保存数据,但具有 wineRegion: 'Bordeaux (Red)' 的多个对象只会在输出数组中出现一次,而 totalPrice 将是所有 totalPrice 值的总和。因此遍历数组,如果 regionName 存在重复值,则增加 totalPrice。

我尝试过使用 reduce 但无法自己解决这个问题。

let newArr = new Map();
data.forEach(x=>{
    if(newArr.has(x.wineRegion)){
        let existingData = newArr.get(x.wineRegion);
        existingData.totalPrice+=x.totalPrice;
    }else{
        newArr.set(x.wineRegion, x);
    }
})

let result = Array.from(newArr.values());

const data = [ { wineName: 'Château Pape-Clément', wineRegion: 'Bordeaux (Red)', unitSize: '12x75cl', wineVintage: 2010, qty: 1, totalPrice: 1650, }, { wineName: 'Château Beauséjour Duffau-Lagarrosse', wineRegion: 'Bordeaux (Red)', unitSize: '6x75cl', wineVintage: 2010, qty: 1, totalPrice: 1400, }, { wineName: 'Vosne-Romanée, Berthaut', wineRegion: 'Burgundy (Red)', unitSize: '12x75cl', wineVintage: 2017, qty: 1, totalPrice: 510, }, { wineName: 'Mazis-Chambertin, Faiveley', wineRegion: 'Burgundy (Red)', unitSize: '6x75cl', wineVintage: 2018, qty: 1, totalPrice: 790, }, { wineName: 'Gevrey-Chambertin Les Champeaux, O. Bernstein', wineRegion: 'Burgundy (Red)', unitSize: '3x75cl', wineVintage: 2019, qty: 1, totalPrice: 675, }, { wineName: 'Latricières-Chambertin, J. M. Fourrier', wineRegion: 'Burgundy (Red)', unitSize: '6x75cl', wineVintage: 2017, qty: 1, totalPrice: 1050, }, { wineName: 'Corton Rognet, Taupenot-Merme', wineRegion: 'Burgundy (Red)', unitSize: '6x75cl', wineVintage: 2019, qty: 1, totalPrice: 600, }, { wineName: 'Corton-Charlemagne, Ponsot', wineRegion: 'Burgundy (White)', unitSize: '6x75cl', wineVintage: 2012, qty: 1, totalPrice: 980, }, { wineName: 'Bollinger Grand Année', wineRegion: 'Champagne ', unitSize: '6x75cl', wineVintage: 2008, qty: 1, totalPrice: 675, }, { wineName: "L'Astre, David Léclapart", wineRegion: 'Champagne ', unitSize: '6x75cl', wineVintage: 2013, qty: 1, totalPrice: 540, }, { wineName: 'Brunello di Montalcino Madonna delle Grazie, Marroneto', wineRegion: 'Italy ', unitSize: '6x75cl', wineVintage: 2012, qty: 1, totalPrice: 945, }, ];

let newArr = new Map();
data.forEach(x=>{
    if(newArr.has(x.wineRegion)){
        let existingData = newArr.get(x.wineRegion);
        existingData.totalPrice+=x.totalPrice;
    }else{
        newArr.set(x.wineRegion, x);
    }
})

let result = Array.from(newArr.values());

console.log(result);

您可以将 reduce()Map 对象一起使用。

data.reduce() 将 return 一个 Map 对象,您可以使用 .values() 获取 Map 对象的值。

然后可以使用展开运算符...获取数组中Map对象的值

简而言之:

const output = [...data.reduce().values()]

const data = [ { wineName: "Château Pape-Clément", wineRegion: "Bordeaux (Red)", unitSize: "12x75cl", wineVintage: 2010, qty: 1, totalPrice: 1650, }, { wineName: "Château Beauséjour Duffau-Lagarrosse", wineRegion: "Bordeaux (Red)", unitSize: "6x75cl", wineVintage: 2010, qty: 1, totalPrice: 1400, }, { wineName: "Vosne-Romanée, Berthaut", wineRegion: "Burgundy (Red)", unitSize: "12x75cl", wineVintage: 2017, qty: 1, totalPrice: 510, }, { wineName: "Mazis-Chambertin, Faiveley", wineRegion: "Burgundy (Red)", unitSize: "6x75cl", wineVintage: 2018, qty: 1, totalPrice: 790, }, { wineName: "Gevrey-Chambertin Les Champeaux, O. Bernstein", wineRegion: "Burgundy (Red)", unitSize: "3x75cl", wineVintage: 2019, qty: 1, totalPrice: 675, }, { wineName: "Latricières-Chambertin, J. M. Fourrier", wineRegion: "Burgundy (Red)", unitSize: "6x75cl", wineVintage: 2017, qty: 1, totalPrice: 1050, }, { wineName: "Corton Rognet, Taupenot-Merme", wineRegion: "Burgundy (Red)", unitSize: "6x75cl", wineVintage: 2019, qty: 1, totalPrice: 600, }, { wineName: "Corton-Charlemagne, Ponsot", wineRegion: "Burgundy (White)", unitSize: "6x75cl", wineVintage: 2012, qty: 1, totalPrice: 980, }, { wineName: "Bollinger Grand Année", wineRegion: "Champagne ", unitSize: "6x75cl", wineVintage: 2008, qty: 1, totalPrice: 675, }, { wineName: "L'Astre, David Léclapart", wineRegion: "Champagne ", unitSize: "6x75cl", wineVintage: 2013, qty: 1, totalPrice: 540, }, { wineName: "Brunello di Montalcino Madonna delle Grazie, Marroneto", wineRegion: "Italy ", unitSize: "6x75cl", wineVintage: 2012, qty: 1, totalPrice: 945, }, ];

const o = [
  ...data
    .reduce((a, b) => {
      if (a.has(b.wineRegion)) {
        const obj = a.get(b.wineRegion);
        obj.totalPrice += b.totalPrice;
        a.set(b.wineRegion, obj);
      } else {
        a.set(b.wineRegion, {
          wineRegion: b.wineRegion,
          totalPrice: b.totalPrice,
        });
      }
      return a;
    }, new Map())
    .values(),
];

console.log(o);