Javascript 按角度分组

Javascript 按角度分组,javascript,angularjs,Javascript,Angularjs,我有以下JSON格式,我希望通过集群和idc分组将“虚拟”和“物理”对象组合成一个 [ { "_id": { "cluster": 1, "idc": "LH8", "type": "Virtual" }, "SumCores": 112, "SumMemory": 384 }, { "_id": { "cluster": 1, "idc": "LH8", "t

我有以下JSON格式,我希望通过集群和idc分组将“虚拟”和“物理”对象组合成一个

[
{
    "_id": {
        "cluster": 1,
        "idc": "LH8",
        "type": "Virtual"
    },
    "SumCores": 112,
    "SumMemory": 384
},
{
    "_id": {
        "cluster": 1,
        "idc": "LH8",
        "type": "Physical"
    },
    "SumCores": 192,
    "SumMemory": 768
},
{
    "_id": {
        "cluster": 2,
        "idc": "LH8",
        "type": "Virtual"
    },
    "SumCores": 232,
    "SumMemory": 469
},
{
    "_id": {
        "cluster": 2,
        "idc": "LH8",
        "type": "Virtual"
    },
    "SumCores": 256,
    "SumMemory": 1024
}
目前,我已使用ng repeat将所有输出输出到屏幕:

<div ng-repeat="item in servers | orderBy:['idc','cluster'] "><p>IDC:{{item._id.idc}} - Cluster: {{item._id.cluster}} - Type: {{item._id.type}} - Sum Cores: {{ item.SumCores }} </p></div>
理想情况下,我希望将其分组到一个表中,并将其作为理想格式:

+---------+--------------------+--------------------+
| Cluster |         LH5        |         LH8        |
+---------+--------------------+--------------------+
|         | Physical | Virtual | Physical | Virtual |
+---------------------------------------------------+
|       1 |   Value  |  Value  |  Value   |  Value  |
|       2 |   Value  |  Value  |  Value   |  Value  |
|       3 |   Value  |  Value  |  Value   |  Value  |
|       4 |   Value  |  Value  |  Value   |  Value  |
+---------+----------+---------+----------+---------+
显然,这里的数据比我的样本中的数据要多得多,而这个值将代表SumCores

如果您认为在那里进行更改会更好,我也可以访问控制器:

Machine.aggregate(  [ { $match : {  $and: [  {"idc": req.query.idc }, {"customer":req.query.customer} ] } } ,{"$group":{_id: {"cluster":"$cluster","idc":"$idc","type":"$type"},"SumCores":{"$sum":"$cores"},"SumMemory": { "$sum":"$memory" }}}, { $sort : { idc : -1, cluster: 1 } } ]).exec(function(err, agg) {

res.json(agg);

});

在这里摆弄:

我已经把你的摆弄弄好了,现在我使用下划线.js根据你的示例表对你的数据进行分组和过滤

它非常基本,并且使用嵌套表。例如,您应该能够通过允许用户更改列表的顺序对其进行自定义

代码示例:

    var lhSortedList = _.groupBy(servers, function(item) {
        return item._id.idc;
    });
    $scope.lh8Virtual = _.filter(lhSortedList['LH8'], function(item) {
        return item._id.type === 'Virtual';   
    });
以下是如何动态执行所需操作的概述:

    var lhList = ['LH5', 'LH8']; // sourced from server ?


    var lhSortedList = _.groupBy(servers, function(item) {
        return item._id.idc;
    });

    $scope.lhData = {};

    lhList.forEach(function(lhName) {
        $scope.lhData[lhName + 'Virtual'] = _.filter(lhSortedList[lhName], function(item) {
            return item._id.type === 'Virtual';   
        });
        $scope.lhData[lhName + 'Physical'] = _.filter(lhSortedList[lhName], function(item) {
            return item._id.type === 'Physical';   
        });
    });

拉一把小提琴会很有用helpful@Jax-为你添加了提琴:)我不久前做了一个代码笔,展示了如何对事物进行分组@stackg91-谢谢,但我不完全确定如何在我的情况下实现这一点谢谢,尽管“idc”是动态的,不应该硬编码。解决方案可以在所有事物都是动态的情况下工作。类型:物理和虚拟是唯一的固定值。或者,您可以维护一个单独的IDC名称列表,并对其进行迭代…您能给我一个如何使其动态化的示例吗?我以前从未真正使用过angular或下划线,所以我对这种语言相当陌生。我在这里添加了一个关于如何动态执行的概述—我现在没有时间继续。阅读所需的主题,并考虑使用简化的表结构来开始…
    var lhList = ['LH5', 'LH8']; // sourced from server ?


    var lhSortedList = _.groupBy(servers, function(item) {
        return item._id.idc;
    });

    $scope.lhData = {};

    lhList.forEach(function(lhName) {
        $scope.lhData[lhName + 'Virtual'] = _.filter(lhSortedList[lhName], function(item) {
            return item._id.type === 'Virtual';   
        });
        $scope.lhData[lhName + 'Physical'] = _.filter(lhSortedList[lhName], function(item) {
            return item._id.type === 'Physical';   
        });
    });