循环遍历对象数组,并根据共享属性值分组/递增对象值



我正在考虑通过这个数据对象数组循环的最佳方式,如果wineRegion名称出现不止一次,我希望输出数组保存这个值一次,totalPrice值增加到所有匹配对象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,但无法自己解决这个问题。

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

data.reduce()将返回一个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);

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);

最新更新