提问人:grojas123 提问时间:5/6/2022 更新时间:5/6/2022 访问量:399
GroupBY 在对象数组中使用 alaSQL
GroupBY with alaSQL in an array of Objects
问:
我需要帮助来实现对象数组的这种转换。
我正在使用 alaSQL。
我需要按玩家进行分组。
我试过了,但我得到了这个结果alasql('SELECT score FROM ? WHERE score GROUP BY player',[arrayGamePlayerObjects])
{
"player": {
"id": 5,
"firstName": "Player",
"lastName": "05",
"email": "[email protected]"
}
}
原始对象数组:
[
{
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"},
"score": {"id": 5,"score": 0.5,"finishDate": "2022-05-05T16:12:22.038+00:00",
"game": {"id": 3,"gameName": "Game 03","gameDate": "2022-05-05T18:12:21.049+00:00"},
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"}
}
},
{
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"},
"score": {"id": 6,"score": 0.5,"finishDate": "2022-05-05T16:12:22.038+00:00",
"game": {"id": 3,"gameName": "Game 03","gameDate": "2022-05-05T18:12:21.049+00:00"},
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"}
}
},
{
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"},
"score": {"id": 7,"score": 0.5,"finishDate": "2022-05-05T16:12:22.042+00:00",
"game": {"id": 6,"gameName": "Game 06","gameDate": "2022-05-05T20:12:21.049+00:00"},
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "playe[email protected]"}
}
},
{
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"},
"score": {"id": 8,"score": 0.5,"finishDate": "2022-05-05T16:12:22.042+00:00",
"game": {"id": 6,"gameName": "Game 06","gameDate": "2022-05-05T20:12:21.049+00:00"},
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"}
}
}
我需要的最终结果是:
[
{
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"},
"score01": {"id": 5,"score": 0.5,"finishDate": "2022-05-05T16:12:22.038+00:00",
"game": {"id": 3,"gameName": "Game 03","gameDate": "2022-05-05T18:12:21.049+00:00"},
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"}
},
"score02": {"id": 7,"score": 0.5,"finishDate": "2022-05-05T16:12:22.042+00:00",
"game": {"id": 6,"gameName": "Game 06","gameDate": "2022-05-05T20:12:21.049+00:00"},
"player": {"id": 5,"firstName": "Player","lastName": "05","email": "[email protected]"}
}
},
{
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"},
"score01": {"id": 6,"score": 0.5,"finishDate": "2022-05-05T16:12:22.038+00:00",
"game": {"id": 3,"gameName": "Game 03","gameDate": "2022-05-05T18:12:21.049+00:00"},
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"}
},
"score02": {"id": 8,"score": 0.5,"finishDate": "2022-05-05T16:12:22.042+00:00",
"game": {"id": 6,"gameName": "Game 06","gameDate": "2022-05-05T20:12:21.049+00:00"},
"player": {"id": 6,"firstName": "Player","lastName": "06","email": "[email protected]"}
}
}
]
关于如何获得此结果的任何指南或想法?
答:
0赞
long-blade
5/6/2022
#1
我认为这会有所帮助!
实现
const groupBy = key => array =>
array.reduce((objectsByKeyValue, obj) => {
const value = obj[key];
objectsByKeyValue[value] = (objectsByKeyValue[value] || []).concat(obj);
return objectsByKeyValue;
}, {});
用法
const cars = [
{ brand: 'Audi', color: 'black' },
{ brand: 'Audi', color: 'white' },
{ brand: 'Ferarri', color: 'red' },
{ brand: 'Ford', color: 'white' },
{ brand: 'Peugot', color: 'white' }
];
const groupByBrand = groupBy('brand');
const groupByColor = groupBy('color');
console.log(
JSON.stringify({
carsByBrand: groupByBrand(cars),
carsByColor: groupByColor(cars)
}, null, 2)
);
输出
{
"carsByBrand": {
"Audi": [
{
"brand": "Audi",
"color": "black"
},
{
"brand": "Audi",
"color": "white"
}
],
"Ferarri": [
{
"brand": "Ferarri",
"color": "red"
}
],
"Ford": [
{
"brand": "Ford",
"color": "white"
}
],
"Peugot": [
{
"brand": "Peugot",
"color": "white"
}
]
},
"carsByColor": {
"black": [
{
"brand": "Audi",
"color": "black"
}
],
"white": [
{
"brand": "Audi",
"color": "white"
},
{
"brand": "Ford",
"color": "white"
},
{
"brand": "Peugot",
"color": "white"
}
],
"red": [
{
"brand": "Ferarri",
"color": "red"
}
]
}
}
参考: 从
评论
0赞
grojas123
5/6/2022
我正在探索这个答案 stackoverflow.com/questions/8706735/......。当我对我的问题有具体的答案时,我会发布解决方案。
0赞
grojas123
5/6/2022
#2
我在此查询中使用了 alaSQL 并且工作正常。
alasql('SELECT player->id , ARRAY(_) AS gamePlayer_per_player FROM ? WHERE score GROUP BY player->id',[arrayGamePlayerObjects]);
评论
0赞
grojas123
5/8/2022
顺便说一句,这句话对我不起作用.出于某种原因,给出了未定义的alasql('SELECT player->id as playerid , ARRAY(_) AS gamePlayer_per_player FROM ? WHERE score GROUP BY playerid',[arrayGamePlayerObjects]);
player->id as playerid
评论