为cols添加新的摘要行,共享2个不同cols的相同价位 [英] Add new summary row for cols share the same vales of 2 different cols
本文介绍了为cols添加新的摘要行,共享2个不同cols的相同价位的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个矩阵算法:
输入:
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'],
]
预期输出:
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.
我有一个旧代码:
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
}
它不起作用,因为它只比较一个col(品牌),而不比较Make和Month.
It doesn't work since it only compares one col (Brand), not Make and Month.
推荐答案
您可以使用组合键和销毁数组.
You could use a combined key and a destructed array.
var array = [['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']],
result = [].concat(...array
.reduce((m, [brand, make, month, town, amount]) => {
var key = [brand, make, month].join('|'),
data = m.get(key) || [['S', brand, make, month, '0']];
data.push(['D', data.length, brand, town, amount]);
data[0][4] = (+data[0][4] + +amount).toString();
return m.set(key, data);
}, new Map)
.values()
);
console.log(result.map(a => a.join(' ')));
.as-console-wrapper { max-height: 100% !important; top: 0; }
这篇关于为cols添加新的摘要行,共享2个不同cols的相同价位的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文