1 回答

TA貢獻1788條經驗 獲得超4個贊
您可以使用數組歸約方法來做到這一點。
const data = [
? {
? ? 'Row Labels': '37383783738',
? ? 'ProdCode&Desc': '8H9H89 Homestyle cinnamon cake (200g) 8x120',
? },
? {
? ? 'Row Labels': '37383783738',
? ? 'ProdCode&Desc': '9HD063 Goodness me! Chargrilled bites (20g) 6x30',
? },
? {
? ? 'Row Labels': '83322223733',
? ? 'ProdCode&Desc': '39HSH02 MDS Chargrilled Hot & Spicy (40g) 2x30',
? },
? {
? ? 'Row Labels': '83322223733',
? ? 'ProdCode&Desc': '93JSHS Treasured battered fillet (120g) 6x30',
? },
];
const ret = Object.values(
? data.reduce((prev, c) => {
? ? const p = prev;
? ? const key = c['Row Labels'];
? ? if (!p[key]) p[key] = { ...c };
? ? else
? ? ? p[key] = {
? ? ? ? ...p[key],
? ? ? ? 'ProdCode&Desc': (p[key]['ProdCode&Desc'] += `; ${c['ProdCode&Desc']}`),
? ? ? };
? ? return p;
? }, {})
);
console.log(ret);
添加回答
舉報