使用meteor,我有一个mongo聚合返回一个反应变量,如下所示:
[{
"_id": "game1",
"playerpicks": [
{
"player": "Chris",
"pick": "atsa"
},
{
"player": "al",
"pick": "suh"
},
{
"player": "al",
"pick": "atsh"
},
{
"player": "Chris",
"pick": "sua"
}] },{
"_id": "game2",
"playerpicks": [
{
"player": "al",
"pick": "atsa"
},
{
"player": "Chris",
"pick": "suh"
},
{
"player": "Chris",
"pick": "atsh"
},
{
"player": "al",
"pick": "sua"
}] }, .... ]
我需要将这些数据转换为理想情况下的网格:
GAME al Chris
game1 suh/atsh sua/atsa
game2 sua/atsa suh/atsh
假设这是一个非常重要的挑战,我可以做出像这样的工作:
game 1 al: suh/atsh Chris: sua/atsa
game 2 al: sua/atsa Chris: suh/atsh
我看到Mongo要求排序发生在客户端,因此我专注于使用下划线,但即使在嵌套数组的_.sortBy或_.groupBy的初始步骤中也无法得到任何结果" playerpicks"
我尝试如下:
_.each(monArr, function(obj){ _.each(obj.playerpicks, function(nst){ _.groupBy( nst.player ) }) })
以及:
_.groupBy(monArr, function(obj){return obj.playerpicks.player})
但我离开了。没有数据返回。任何建议非常感谢
答案 0 :(得分:1)
这是一个使用无数下划线函数的解决方案,分解为离散的块,希望它更具可读性:
// helper function to combine a player's picks into a string delimited by '/'
let combinePicks = player => _.pluck(player, 'pick').join('/');
// helper function to group a game by a player
let groupGame = game => _.chain(game.playerpicks)
.groupBy('player')
.mapObject(combinePicks)
.value();
// map the data to create the object in the required form
let result = _.map(data, game => _.extend({GAME: game._id}, groupGame(game) ));
对于不支持mapObject
(1.8之前版本)的早期版本的下划线,使用reduce可以实现相同的结果:
let combinePicks = function(memo, player, key){
memo[key] = _.pluck(player, 'pick').join('/');
return memo;
}
let groupGame = game => _.chain(game.playerpicks)
.groupBy('player')
.reduce(combinePicks, {})
.value();
var data = [{
"_id": "game1",
"playerpicks": [
{
"player": "Chris",
"pick": "atsa"
},
{
"player": "al",
"pick": "suh"
},
{
"player": "al",
"pick": "atsh"
},
{
"player": "Chris",
"pick": "sua"
}] },{
"_id": "game2",
"playerpicks": [
{
"player": "al",
"pick": "atsa"
},
{
"player": "Chris",
"pick": "suh"
},
{
"player": "Chris",
"pick": "atsh"
},
{
"player": "al",
"pick": "sua"
}] }
];
let combinePicks = player => _.pluck(player, 'pick').join('/');
let groupGame = game => _.chain(game.playerpicks)
.groupBy('player')
.mapObject(combinePicks)
.value();
let result = _.map(data, game => _.extend({GAME: game._id}, groupGame(game) ));
document.getElementById('result').textContent = JSON.stringify(result);
<script src="https://cdnjs.cloudflare.com/ajax/libs/underscore.js/1.8.3/underscore-min.js"></script>
<p>
<pre id="result"></pre>
</p>