尽管linq的答案很有趣,但它也很笨重。我的方法有些不同:
var DataGrouper = (function() {
var has = function(obj, target) {
return _.any(obj, function(value) {
return _.isEqual(value, target);
});
};
var keys = function(data, names) {
return _.reduce(data, function(memo, item) {
var key = _.pick(item, names);
if (!has(memo, key)) {
memo.push(key);
}
return memo;
}, []);
};
var group = function(data, names) {
var stems = keys(data, names);
return _.map(stems, function(stem) {
return {
key: stem,
vals:_.map(_.where(data, stem), function(item) {
return _.omit(item, names);
})
};
});
};
group.register = function(name, converter) {
return group[name] = function(data, names) {
return _.map(group(data, names), converter);
};
};
return group;
}());
DataGrouper.register("sum", function(item) {
return _.extend({}, item.key, {Value: _.reduce(item.vals, function(memo, node) {
return memo + Number(node.Value);
}, 0)});
});
您可以在JSBin上看到它的运行情况。
has
尽管我可能会遗漏它,但在Underscore中我看不到能做什么的任何东西。它与几乎相同_.contains
,但是用于_.isEqual
而不是===
用于比较。除此之外,其余的都是特定于问题的,尽管尝试是通用的。
现在DataGrouper.sum(data, ["Phase"])
返回
[
{Phase: "Phase 1", Value: 50},
{Phase: "Phase 2", Value: 130}
]
并DataGrouper.sum(data, ["Phase", "Step"])
返回
[
{Phase: "Phase 1", Step: "Step 1", Value: 15},
{Phase: "Phase 1", Step: "Step 2", Value: 35},
{Phase: "Phase 2", Step: "Step 1", Value: 55},
{Phase: "Phase 2", Step: "Step 2", Value: 75}
]
但是sum
这里仅仅是一种潜在的功能。您可以根据需要注册其他人:
DataGrouper.register("max", function(item) {
return _.extend({}, item.key, {Max: _.reduce(item.vals, function(memo, node) {
return Math.max(memo, Number(node.Value));
}, Number.NEGATIVE_INFINITY)});
});
现在DataGrouper.max(data, ["Phase", "Step"])
会回来
[
{Phase: "Phase 1", Step: "Step 1", Max: 10},
{Phase: "Phase 1", Step: "Step 2", Max: 20},
{Phase: "Phase 2", Step: "Step 1", Max: 30},
{Phase: "Phase 2", Step: "Step 2", Max: 40}
]
或者如果您注册了此:
DataGrouper.register("tasks", function(item) {
return _.extend({}, item.key, {Tasks: _.map(item.vals, function(item) {
return item.Task + " (" + item.Value + ")";
}).join(", ")});
});
然后打电话DataGrouper.tasks(data, ["Phase", "Step"])
给你
[
{Phase: "Phase 1", Step: "Step 1", Tasks: "Task 1 (5), Task 2 (10)"},
{Phase: "Phase 1", Step: "Step 2", Tasks: "Task 1 (15), Task 2 (20)"},
{Phase: "Phase 2", Step: "Step 1", Tasks: "Task 1 (25), Task 2 (30)"},
{Phase: "Phase 2", Step: "Step 2", Tasks: "Task 1 (35), Task 2 (40)"}
]
DataGrouper
本身就是一个功能。您可以使用数据和要分组的属性列表来调用它。它返回一个数组,其元素是具有两个属性的对象:key
是分组属性的集合,vals
是包含不在键中的其余属性的对象的数组。例如,DataGrouper(data, ["Phase", "Step"])
将产生:
[
{
"key": {Phase: "Phase 1", Step: "Step 1"},
"vals": [
{Task: "Task 1", Value: "5"},
{Task: "Task 2", Value: "10"}
]
},
{
"key": {Phase: "Phase 1", Step: "Step 2"},
"vals": [
{Task: "Task 1", Value: "15"},
{Task: "Task 2", Value: "20"}
]
},
{
"key": {Phase: "Phase 2", Step: "Step 1"},
"vals": [
{Task: "Task 1", Value: "25"},
{Task: "Task 2", Value: "30"}
]
},
{
"key": {Phase: "Phase 2", Step: "Step 2"},
"vals": [
{Task: "Task 1", Value: "35"},
{Task: "Task 2", Value: "40"}
]
}
]
DataGrouper.register
接受一个函数并创建一个新函数,该函数接受初始数据和要分组的属性。然后,此新函数采用上述输出格式,并针对每个函数依次运行您的函数,并返回一个新数组。生成的函数DataGrouper
根据您提供的名称存储为的属性,如果只需要本地引用,也将返回该函数。
嗯,这有很多解释。我希望代码相当简单明了!