考虑以下数据:
[
{ time: '5:00', name: 'bran', fruit: 'pear', numEaten: 3},
{ time: '5:00', name: 'rickon', fruit: 'apple', numEaten: 2},
{ time: '6:00', name: 'bran', fruit: 'apple', numEaten: 5},
{ time: '6:00', name: 'rickon', fruit: 'grape', numEaten: 1},
{ time: '6:00', name: 'bran', fruit: 'pear', numEaten: 2},
{ time: '6:00', name: 'eddard', fruit: 'pear', numEaten: 2},
{ time: '7:00', name: 'rickon', fruit: 'apple', numEaten: 7}
]
我想要做的是删除一个列,然后添加' numEaten'所有具有匹配列的行。所以想象一下:当你吃水果时,你实际上并不关心,你只想知道谁吃了多少。所以输出表看起来像:
[
{name: 'bran', fruit: 'pear', numEaten: 5},
{name: 'bran', fruit: 'apple', numEaten: 2},
{name: 'rickon', fruit: 'apple', numEaten: 9},
{name: 'rickon', fruit: 'grape', numEaten: 1},
{name: 'eddard', fruit: 'pear', numEaten: 2},
]
我一直在查看下划线中的各种javascript数组原型函数和扩展,但我无法看到一种特别优雅的方法来执行此操作。我想要一个原型功能:
function aggregate(data, column, aggregateColumn) // aggregate(data, 'time', 'numEaten')
将执行此操作。从概念上讲,我正在考虑为每个不 _.groupBy()
或column
的列运行aggregateColumn
,但这样做似乎有点蠢蠢欲动。还有更好的方法吗?
修改
似乎没有针对此问题的单行解决方案:在收集以下解决方案的反馈后,发布我想出的内容。请注意,与原始问题不同,这会使列保持,而不是删除,并适用于任何架构。
var aggregate = function(data, aggregateColumn, keepColumns) {
keepColumns = keepColumns || [];
if(!Array.isArray(keepColumns)) {
keepColumns = [ keepColumns ];
}
var removeColumns = _.difference(_.keys(data[0]), keepColumns.concat(aggregateColumn));
var grouped = _.groupBy(data, function(d) {
return _.reduce(keepColumns, function(o, col) {
return o + d[col] + '-';
}, '');
});
return _.map(grouped, function(mapData) {
var reduced = _.reduce(keepColumns, function(o, col) {
o[col] = mapData[0][col];
return o;
}, {}
);
reduced[aggregateColumn] = _.reduce(mapData, function(o, aggrData) {
return o + aggrData[aggregateColumn];
}, 0
);
return reduced;
});
}
答案 0 :(得分:2)
这是在下划线中执行此操作的一种方法
让我们定义像
这样的初始数据var data = [
{ time: '5:00', name: 'bran', fruit: 'pear', numEaten: 3},
{ time: '5:00', name: 'rickon', fruit: 'apple', numEaten: 2},
{ time: '6:00', name: 'bran', fruit: 'apple', numEaten: 5},
{ time: '6:00', name: 'rickon', fruit: 'grape', numEaten: 1},
{ time: '6:00', name: 'bran', fruit: 'pear', numEaten: 2},
{ time: '6:00', name: 'eddard', fruit: 'pear', numEaten: 2},
{ time: '7:00', name: 'rickon', fruit: 'apple', numEaten: 7}
]
然后,通过加入来创建基于name
和fruit
的群组。
var groups = _.groupBy(data, function(value){
return value.name+ '#' + value.fruit;
});
我们稍后会在汇总时使用此自定义sum
功能。
function sum(numbers) {
return _.reduce(numbers, function(result, current) {
return result + parseFloat(current);
}, 0);
}
现在,通过提取map
并取出numEaten
sum
这些群组
var out = _.map(groups, function(group){
return {
name: group[0].name,
fruit: group[0].fruit,
numEaten: sum(_.pluck(group, 'numEaten'))
}
});
最后,我们得到了像
这样的输出out
[
{name: 'bran', fruit: 'pear', numEaten: 5},
{name: 'bran', fruit: 'apple', numEaten: 5},
{name: 'rickon', fruit: 'apple', numEaten: 9},
{name: 'rickon', fruit: 'grape', numEaten: 1},
{name: 'eddard', fruit: 'pear', numEaten: 2},
]
答案 1 :(得分:2)
使用纯JavaScript时,通用解决方案很简单,但我想使用下划线提供此解决方案,因为有时会让人感到兴奋!
由于下划线不提供删除重复项的适当功能,因此我使用_.uniq
与JSON.stringify
函数混合。
以下是成功测试的aggregate
函数
var objs = [
{ time: '5:00', name: 'bran', fruit: 'pear', numEaten: 3},
{ time: '6:00', name: 'bran', fruit: 'pear', numEaten: 2},
{ time: '6:00', name: 'bran', fruit: 'apple', numEaten: 5},
{ time: '5:00', name: 'rickon', fruit: 'apple', numEaten: 2},
{ time: '7:00', name: 'rickon', fruit: 'apple', numEaten: 7},
{ time: '6:00', name: 'rickon', fruit: 'grape', numEaten: 1},
{ time: '6:00', name: 'eddard', fruit: 'pear', numEaten: 2}
];
function aggregate(data, column, aggregateColumn){
var res=[];
_.map(data, function(item){
var comparer={},
compared={};
for(var k in item){
if(k!=column){
compared[k]=item[k];
if(k!=aggregateColumn)
comparer[k]=item[k];
}
}
_.each(_.where(_.without(data,item), comparer),function(aggregable){
compared[aggregateColumn]+=aggregable[aggregateColumn];
return compared;
});
res.push(compared);
});
return _.uniq(res,function(item){return JSON.stringify(item);})
}
///usage
var o=aggregate(objs, 'time', 'numEaten');
console.log({'o':o});
答案 2 :(得分:1)
你在谈论“专栏”的事实表明,当你正在处理一系列字符串地图时,你会想到一张桌子。
由于JavaScript是基于原型的事实,因此没有“漂亮”或开箱即用的解决方案(不仅是,而且还有)。
您可以选择for循环和Array.forEach。我更喜欢前者 此外,我在这里返回一个新数组,而不是原位修改旧数组。
function aggregate(data, column, aggregateColumn)
{
var array = [];
// Just work the array
for(var i = 0; i < data.length; i++)
{
var currentOld = data[i];
var found = false;
// Label the loop, so we can control it
outside:
// Check if the current type already exists in the new array
for(var j = 0; j < array.length; j++)
{
var currentNew = array[j];
// Check if all properties match
for(var property in currentOld)
{
// Skip properties that match column or aggregateColumn
if(property == column || property == aggregateColumn)
{
continue;
}
// Now check if their values match
if(currentOld[property] != currentNew[property])
{
// If they don't match, continue the outer loop
continue outside;
}
}
// At this point, all properties matched, so we aggregate
currentNew[aggregateColumn] += currentOld[aggregateColumn];
// Set the flag to indicate that we found it
found = true;
// And end the loop
break;
}
// If the current type is not yet in the new array, we need to put it there
if(!found)
{
// Create a copy of it (assuming your data are trivial objects)
var copy = JSON.parse(JSON.stringify(currentOld));
// Remove your "column"
delete copy[column];
// And add it
array.push(copy);
}
}
return array;
}
测试函数输出你想要的相同数组,只是按照不同的顺序,因为它保持原始数组的顺序而不是排序。
我假设您知道如何对数组进行排序。 ;)
答案 3 :(得分:1)
使用John Galt的优秀答案中的sum函数,这是一个通用版本
function aggregate(data, aggregateColumn, keepColumns){
var groups = _.groupBy(data, function(item){
return _.values(_.pick(item, keepColumns)).join('#')
});
return _.map(groups, function(group){
return _.extend( _.pick(group[0], keepColumns),
_.object([aggregateColumn], [sum(_.pluck(group, aggregateColumn))]));
});
}