I have a matrix algorithm:
Input:
const input = [
['Camry', 'Toyota', 'Jan', 'Nowhere Town', '50'],
['Camry', 'Toyota', 'Feb', 'Nowhere Town', '70'],
['Camry', 'Toyota', 'Jan', 'Random City', '3000'],
['Prius', 'Toyota', 'Jan', 'Nowhere Town', '60'],
['Prius', 'Toyota', 'Jan', 'Random Town', '60'],
['Prius', 'Toyota', 'Mar', 'Nowhere Town', '50'],
['Civic', 'Honda', 'Jan', 'Nowhere Town', '10'],
['Civic', 'Honda', 'Feb', 'Nowhere Town', '10'],
['Civic', 'Honda', 'Mar', 'Random Town', '10'],
['Civic', 'Honda', 'Mar', 'Random Town', '20'],
]
Expected output:
const output = [
['S', 'Camry', 'Toyota', 'Jan', '3050'],
['D', 1, 'Camry', 'Nowhere Town', '50'],
['D', 2, 'Camry', 'Random City', '3000'],
['S', 'Camry', 'Toyota', 'Feb', '70'],
['D', 1, 'Camry', 'Nowhere Town', '70'],
['S', 'Prius', 'Toyota', 'Jan', '120'],
['D', 1, 'Prius', 'Nowhere Town', '60'],
['D', 2, 'Prius', 'Random Town', '60'],
['S', 'Prius', 'Toyota', 'Mar', '50'],
['D', 1, 'Prius', 'Nowhere Town', '50'],
['S', 'Civic', 'Honda', 'Jan', '10'],
['D', 1, 'Civic', 'Nowhere Town', '10'],
['S', 'Civic', 'Honda', 'Feb', '10'],
['D', 1, 'Civic', 'Nowhere Town', '10'],
['S', 'Civic', 'Honda', 'Mar', '20'],
['D', 1, 'Civic', 'Random Town', '10'],
['D', 2, 'Civic', 'Random Town', '10'],
]
In words: If rows contain the same Brand, the same Make and the same Month add a Summary Row on top with total sales and add listed order for each details row.
I have an old code:
const groupReport = arr => {
let grouped = [].concat(...arr.reduce((acc, cur) => {
var data = acc.get(cur[1]) || [['P', cur[1], '0']]
data.push(['D', data.length, cur[0], cur[3], cur[4]])
data[0][2] = (+data[0][2] + +cur[4]).toString()
return acc.set(cur[0], data);
}, new Map)
.values()
)
return grouped
}
It doesn't work since it only compares one col (Brand), not Make and Month.