我有一个JSON文件,我希望根据其中的三个字段对该JSON进行分组。
JSON如下(当然还有更多内容):
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<div class="form-group row">
<select name="count_pax" id="count_paxx" class="form-control col-md-9" />
<option value="">Select how many people</option>
<option value="1">1 pax</option>
<option value="2">2 pax</option>
<option value="3">3 pax</option>
<option value="4">4 pax</option>
<option value="5">5 pax</option>
<option value="6">6 pax</option>
<option value="7">7 pax</option>
<option value="8">8 pax</option>
<option value="9">9 pax</option>
<option value="10">10 pax</option>
<option value="11">11 pax</option>
<option value="12">12 pax</option>
</select>
</div>
<div class="form-group row" id="demo_paxx">
</div>
请求的结果(使用switch result.rawValue {
case MFMailComposeResult.cancelled.rawValue:
print("Mail cancelled")
case MFMailComposeResult.saved.rawValue:
print("Mail saved")
case MFMailComposeResult.sent.rawValue:
print("Mail sent")
case MFMailComposeResult.failed.rawValue:
print("Mail sent failure: \(error?.localizedDescription)")
default:
break
}
或{
"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
答案 0 :(得分:1)
您可以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);