javascript - 按数组分组
问题描述
我有一个 JSON 文件,我希望根据其中的三个字段对这个 JSON 进行分组。
JSON 如下所示(当然还有更多项目):
{
"Racename": "10KM",
"Category": 34,
"Gender": "Male",
"Work": "Google",
"FullName": "Dave Happner",
"Rank": 1,
"Ponit": 1,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
}
请求的结果(使用underscore
or lodash
)是:
[
{
"Racename" : "10KM",
"Category": "34",
"Gender": "Male",
runner : [
{
"Work": "Google",
"FullName": "Dave Happner",
"Rank": 1,
"Ponit": 1,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
}]
解决方案
您可以reduce
进入由字符串索引的对象,该字符串由 、 和 组成Racename
,Category
并由Gender
值中不存在的字符连接,例如_
. 例如,您在问题中的输入将产生一个键为 的对象10KM_34_Male
。在每次迭代中,检查构造的键是否存在 - 如果不存在,则使用空runner
数组创建对象。然后,推送到runner
数组。
完成后reduce
,您可以获取对象的值以获得所需的数组输出:
const input = [{
"Racename": "10KM",
"Category": 34,
"Gender": "Male",
"Work": "Google",
"FullName": "Dave Happner",
"Rank": 1,
"Ponit": 1,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
}];
const outputObj = input.reduce((a, { Racename, Category, Gender, ...rest }) => {
const key = [Racename, Category, Gender].join('_');
if (!a[key]) {
a[key] = { Racename, Category, Gender, runner: [] };
}
a[key].runner.push(rest);
return a;
}, {});
const output = Object.values(outputObj);
console.log(output);
或者,使用更大的输入:
const input = [{
"Racename": "10KM",
"Category": 34,
"Gender": "Male",
"Work": "Google",
"FullName": "Dave Happner",
"Rank": 1,
"Ponit": 1,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
},{
"Racename": "10KM",
"Category": 34,
"Gender": "Male",
"Work": "Amazon",
"FullName": "Bob Joe",
"Rank": 12,
"Ponit": 2,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
},{
"Racename": "20KM",
"Category": 40,
"Gender": "Male",
"Work": "Google",
"FullName": "Dave Happner",
"Rank": 1,
"Ponit": 1,
"Numparticipant": 0,
"rankparticipant": 0,
"precentagePart": "0",
"NumRaces": 1,
"RaceTime": "2018-10-18T00:34:20",
"rankCat": 1,
"PointCat": 1,
"RaceDate": "2018-10-05"
}
];
const outputObj = input.reduce((a, { Racename, Category, Gender, ...rest }) => {
const key = [Racename, Category, Gender].join('_');
if (!a[key]) {
a[key] = { Racename, Category, Gender, runner: [] };
}
a[key].runner.push(rest);
return a;
}, {});
const output = Object.values(outputObj);
console.log(output);
推荐阅读
- mysql - 有没有办法在 IN() 语句中“捕获”所有跳过的项目?
- ios - 以编程方式在 UITableViewCell 中添加的约束的奇怪行为 迅速
- airflow - on_failure_callback 在气流 DAG() 中不起作用
- python - 检查体积 (.ply) 是否包含 3D 点
- python - 在线性和非线性值之间进行插值
- python - PIP 下载包很慢
- macos - macOS 上的 wget 如何处理证书?
- json - Jira 添加内部注释 JSON 语法
- javascript - 将 JavaScript 数组转换为 JSON 对象
- python - 结合PDF的文件名