我在对以下数组进行分组时遇到麻烦:
{selectedDay: "09", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020}
{selectedDay: "10", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020}
{selectedDay: "11", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020}
{selectedDay: "12", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020}
{selectedDay: "13", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020}
{selectedDay: "13", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020}
{selectedDay: "14", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020}
{selectedDay: "15", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020}
{selectedDay: "16", selectedMonth: "April", paidPeriod: "Period 1", paidMonth: "May", paidYear: 2020}
{selectedDay: "17", selectedMonth: "April", paidPeriod: "Period 1", paidMonth: "May", paidYear: 2020}
我打算按selectedMonth => paidPeriod => selectedDay分组。
如何删除重复的数据?
我如何分组?
我需要这种方式,有什么想法吗?
{
"March": {
"Period 2": {
09,
10,
11,
12,
13
}
},
"April":{
"Period 2": {
13,
14,
15
}
},
"May":{
"Period 1": {
16
},
"Period 2":{
17
}
}
}
您可以使用reduce
来完成。
const data = [{selectedDay: "09", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020},
{selectedDay: "10", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020},
{selectedDay: "11", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020},
{selectedDay: "12", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020},
{selectedDay: "13", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020},
{selectedDay: "13", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020},
{selectedDay: "14", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020},
{selectedDay: "15", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020},
{selectedDay: "16", selectedMonth: "April", paidPeriod: "Period 1", paidMonth: "May", paidYear: 2020},
{selectedDay: "17", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "May", paidYear: 2020}];
const res = data.reduce((arr, {selectedDay, paidMonth, paidPeriod}) => {
arr[paidMonth] = arr[paidMonth] || {};
arr[paidMonth][paidPeriod] = arr[paidMonth][paidPeriod] || [];
arr[paidMonth][paidPeriod].push(selectedDay);
return arr;
}, {});
console.log(res);
您可以通过对所需键使用组数组,对所需值使用另一个键来采用动态方法。
var data = [{ selectedDay: "09", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020 }, { selectedDay: "10", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020 }, { selectedDay: "11", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020 }, { selectedDay: "12", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020 }, { selectedDay: "13", selectedMonth: "March", paidPeriod: "Period 2", paidMonth: "March", paidYear: 2020 }, { selectedDay: "13", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020 }, { selectedDay: "14", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020 }, { selectedDay: "15", selectedMonth: "April", paidPeriod: "Period 2", paidMonth: "April", paidYear: 2020 }, { selectedDay: "16", selectedMonth: "April", paidPeriod: "Period 1", paidMonth: "May", paidYear: 2020 }, { selectedDay: "17", selectedMonth: "April", paidPeriod: "Period 1", paidMonth: "May", paidYear: 2020 }],
groups = ['paidMonth', 'paidPeriod'],
value = 'selectedDay',
result = data.reduce((r, o) => {
groups
.reduce((p, k, i, { length }) => {
p[o[k]] = p[o[k]] || (i + 1 === length ? [] : {});
return p[o[k]];
}, r)
.push(o[value]);
return r;
}, {});
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }