gpt4 book ai didi

javascript - 按所有键分组并聚合对象数组

转载 作者:行者123 更新时间:2023-11-29 23:01:14 27 4
gpt4 key购买 nike

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

我可以使用 nest 和 rollup 来做到这一点,但仅限于一个维度。例如下面的示例,按 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>

最佳答案

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

// 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>

关于javascript - 按所有键分组并聚合对象数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55551257/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com