通过所有对象分组并汇总所有对象

时间:2019-04-06 16:46:12

标签: javascript d3.js

我有一个对象数组,每个对象都有多个键,值对。我想按第一个键的值分组,然后得出汇总的平均值和中位数。

我可以使用嵌套和汇总来做到这一点,但只能用于一个维度。例如,下面的示例按winner分组,然后为每个子组找到均值/中位数,但仅在一个维度上,在这种情况下,仅在team.4上。有没有一种方法可以一次汇总所有四个team.1, team.2, team.3, team.4?附带说明,team.1, team.2, team.3, team.4是事先未知的。

我想要的输出(但是非常灵活,只是“很不错”)将是

var avg=[ 
{ 'winner': 'team.1', 'team.1' : 4, 'team.2' : 5.333, 'team.3': 1, 'team.4': 0.666},
{ 'winner': 'team.2', 'team.1' : 6, 'team.2' : 2.5, 'team.3': 6.5, 'team.4': 0.5}
];

,中位数也是如此。

谢谢!

<!DOCTYPE html>
<html>

<head>
    <!--d3 -->
    <script src='https://d3js.org/d3.v4.min.js'></script>
</head>

<body>

<script>
    var data = [];
        data[0] = {'winner': 'team.1', 'team.1':5, 'team.2':4, 'team.3':1, 'team.4':0},
        data[1] = {'winner': 'team.2', 'team.1':5, 'team.2':1, 'team.3':4, 'team.4':1},
        data[2] = {'winner': 'team.2', 'team.1':7, 'team.2':4, 'team.3':9, 'team.4':0},
        data[3] = {'winner': 'team.1', 'team.1':5, 'team.2':8, 'team.3':0, 'team.4':1},
        data[4] = {'winner': 'team.1', 'team.1':2, 'team.2':4, 'team.3':2, 'team.4':1}

        var dim = 'team.4';

        var out = d3.nest()
            .key(function(d) { return d.winner; })
            .rollup(function(v) { return {
                dimension: dim,
                count: v.length,
                median: d3.median(v, function(d) { return d[dim]; }),
                avg: d3.mean(v, function(d) { return d[dim]; })
             };  })
             .entries(data);


         console.log(out);

</script>
</body>

</html>

1 个答案:

答案 0 :(得分:1)

由于具有双重嵌套的数据,因此必须在第一个汇总功能内嵌套另一级汇总功能。因此,您的顶级汇总应具有如下所示的回调:

// Iterate through the object, remove the winner
// That will leave us an object containing team-score key-value pairs
// And then, we flatten the array down to a single dimension:
var teams = v.map(function(team) {
  delete team.winner;
  return d3.entries(team);
}).reduce(function(memo, team) {
  return memo.concat(team);
}, []);

// Generate the summary for the winner group
// We have an array of objects of all the scores of all teams that the winning team has played against
var groupSummary = d3.nest()
  .key(function(d) { return d.key; })
  .rollup(function(w) {
    return {
      count: w.length,
      median: d3.median(w, function(d) {
        return d['value'];
      }),
      avg: d3.mean(w, function(d) {
        return d['value'];
      })
    };
  })
  .entries(teams);

// Return the summary to the top-level rollup
return groupSummary;

<!DOCTYPE html>
<html>

<head>
  <!--d3 -->
  <script src='https://d3js.org/d3.v4.min.js'></script>
</head>

<body>

  <script>
    var data = [];
    data[0] = {
        'winner': 'team.1',
        'team.1': 5,
        'team.2': 4,
        'team.3': 1,
        'team.4': 0
      },
      data[1] = {
        'winner': 'team.2',
        'team.1': 5,
        'team.2': 1,
        'team.3': 4,
        'team.4': 1
      },
      data[2] = {
        'winner': 'team.2',
        'team.1': 7,
        'team.2': 4,
        'team.3': 9,
        'team.4': 0
      },
      data[3] = {
        'winner': 'team.1',
        'team.1': 5,
        'team.2': 8,
        'team.3': 0,
        'team.4': 1
      },
      data[4] = {
        'winner': 'team.1',
        'team.1': 2,
        'team.2': 4,
        'team.3': 2,
        'team.4': 1
      }

    var dim = 'team.4';

    var out = d3.nest()
      .key(function(d) {
        return d.winner;
      })
      .rollup(function(v) {
        var teams = v.map(function(team) {
          delete team.winner;
          return d3.entries(team);
        }).reduce(function(memo, team) {
          return memo.concat(team);
        }, []);
        
        var a = d3.nest()
          .key(function(d) { return d.key; })
          .rollup(function(w) {
            return {
              count: w.length,
              median: d3.median(w, function(d) {
                return d['value'];
              }),
              avg: d3.mean(w, function(d) {
                return d['value'];
              })
            };
          })
          .entries(teams);
   
        return a;
      })
      .entries(data);

    console.log(out);
  </script>
</body>

</html>


另一种(可能更简单)的解决方案是获取对象中的所有键并将它们存储到数组中,并确保遍历所有键(即团队名称)而不是在汇总中返回单个维度。 / ids):

// Generate an array of all team names in the group
var teams = v.reduce(function(memo, d) {
  // Iterate through nested array of objects and get their keys
  // We use reduce here so that we can flatten the 2D array into 1D
  return memo.concat(Object.keys(d));
}, []).filter(function(team) {
  // Remove winner because it is not a "team" per se
  return team !== 'winner';
});

// Now, iterate through all teams and summarize
return teams.map(function(team) {
  return {
    dimension: team,
    count: v.length,
    median: d3.median(v, function(d) {
      return d[team];
    }),
    avg: d3.mean(v, function(d) {
      return d[team];
    })
  };
});

<!DOCTYPE html>
<html>

<head>
  <!--d3 -->
  <script src='https://d3js.org/d3.v4.min.js'></script>
</head>

<body>

  <script>
    var data = [];
    data[0] = {
        'winner': 'team.1',
        'team.1': 5,
        'team.2': 4,
        'team.3': 1,
        'team.4': 0
      },
      data[1] = {
        'winner': 'team.2',
        'team.1': 5,
        'team.2': 1,
        'team.3': 4,
        'team.4': 1
      },
      data[2] = {
        'winner': 'team.2',
        'team.1': 7,
        'team.2': 4,
        'team.3': 9,
        'team.4': 0
      },
      data[3] = {
        'winner': 'team.1',
        'team.1': 5,
        'team.2': 8,
        'team.3': 0,
        'team.4': 1
      },
      data[4] = {
        'winner': 'team.1',
        'team.1': 2,
        'team.2': 4,
        'team.3': 2,
        'team.4': 1
      }

    var dim = 'team.4';

    var out = d3.nest()
      .key(function(d) {
        return d.winner;
      })
      .rollup(function(v) {
        var teams = v.reduce(function(memo, d) {
          return memo.concat(Object.keys(d));
        }, []).filter(function(team) {
          return team !== 'winner';
        });
        
        return teams.map(function(team) {
          return {
            dimension: team,
            count: v.length,
            median: d3.median(v, function(d) {
              return d[team];
            }),
            avg: d3.mean(v, function(d) {
              return d[team];
            })
          };
        });
      })
      .entries(data);


    console.log(out);
  </script>
</body>

</html>