按月对数组进行编号,并在新的月份和年份重置为 1
Numbering array by month and reset to 1 on new month and year
我有一个数组中的数据,其结构如下
[
{createdAt: "2022-03-12T13:15:41.844+00:00", "name": 'adipiscing'},
{createdAt: "2022-03-04T11:56:27.545+00:00", "name": 'consectetur'},
{createdAt: "2021-01-15T09:12:25.418+00:00", "name": 'amet'},
{createdAt: "2021-01-09T09:12:25.418+00:00", "name": 'sit'},
{createdAt: "2021-01-02T08:52:55.418+00:00", "name": 'dolor'},
{createdAt: "2021-12-15T09:12:25.418+00:00", "name": 'ipsum'},
{createdAt: "2021-12-11T08:52:55.418+00:00", "name": 'lorem'},
]
以上数据按 createdAt desc 排序。
目前我像往常一样使用索引数据进行编号,
我想根据 createdAt 列更改编号,但每次更改月份或在新的月份或年份中输入时,数字都会再次重置为 1
谁能帮帮我,万分感谢
以下可能是实现所需 objective 的一种可能解决方案:
代码段
// helper method to get either YYYY-MM (if full=false) or YYYY-MM-DD (otherwise)
const getDtYMD = (dt, full = false) => (
full ? dt.createdAt.split('T')[0] : dt.slice(0, 7)
);
// transform the arr to add a group-id (idGroup)
const sortAndGroup = arr => (
Object.values( // get the values of the result object
[...arr.map(x => ({...x}))].sort( // first sort the input-array reverse-chronological order (latest date at 0)
(a, b) => (getDtYMD(a, true) > getDtYMD(b, true) ? 1 : -1)
).reduce( // use .reduce to iterate over the sorted array
(acc, {createdAt : c, name}) => ({
...acc, // safe-keep existing accumulator object
[getDtYMD(c)]: ([
...(acc[getDtYMD(c)] || []),
{ // append new object to value array for key YYYY-MM
idGroup: (acc[getDtYMD(c)]?.length || 0) + 1,
createdAt: c, // update 'idGroup' based on existing value-array's length
name
}
])
}),
{}
)
).flat().sort(
(a, b) => (
getDtYMD(a, true) > getDtYMD(b, true)
? -1
: getDtYMD(a, true) < getDtYMD(b, true)
? 1
: a.idGroup > b.idGroup ? -1 : 1
)
)
);
const unsortedRawDataArray = [
{'createdAt': "2022-03-12T13:15:41.844+00:00", "name": '1 adipiscing'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '1 consectetur'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '2 consectetur'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '3 consectetur'},
{'createdAt': "2021-01-15T09:12:25.418+00:00", "name": '1 amet'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '1 sit'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '2 sit'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '3 sit'},
{'createdAt': "2021-01-02T08:52:55.418+00:00", "name": '1 dolor'},
{'createdAt': "2021-01-02T08:52:55.418+00:00", "name": '2 dolor'},
{'createdAt': "2021-12-15T09:12:25.418+00:00", "name": '1 ipsum'},
{'createdAt': "2021-12-11T08:52:55.418+00:00", "name": '1 lorem'},
];
console.log('sorted-grouped: ', sortAndGroup(unsortedRawDataArray));
console.log('original-array: ', unsortedRawDataArray);
说明
步骤说明已作为内联注释添加到上述代码片段中。
编辑
when there is data that has the same date/day the order returns to 1
again,
数组中的数据已更新为具有相同日期的多个数据,并且 idGroup
显示为未重置。
besides is there a way to reverse the number without changing the
index, for example from 1-3 to 3-1.
这已在更新的代码段中完成。在 .flat()
之后,我们添加另一个 .sort()
,它不仅考虑日期,还考虑 idGroup
,以使数字从 3-1(反向)开始。
编辑 2
why after calling the sortAndGroup() function the data from
unsortedRawDataArray also changes
此问题已修复。传入的arr
(数组)是通过.sort
直接变异的,最新版本的答案复制了arr
数组。
我有一个数组中的数据,其结构如下
[
{createdAt: "2022-03-12T13:15:41.844+00:00", "name": 'adipiscing'},
{createdAt: "2022-03-04T11:56:27.545+00:00", "name": 'consectetur'},
{createdAt: "2021-01-15T09:12:25.418+00:00", "name": 'amet'},
{createdAt: "2021-01-09T09:12:25.418+00:00", "name": 'sit'},
{createdAt: "2021-01-02T08:52:55.418+00:00", "name": 'dolor'},
{createdAt: "2021-12-15T09:12:25.418+00:00", "name": 'ipsum'},
{createdAt: "2021-12-11T08:52:55.418+00:00", "name": 'lorem'},
]
以上数据按 createdAt desc 排序。
目前我像往常一样使用索引数据进行编号, 我想根据 createdAt 列更改编号,但每次更改月份或在新的月份或年份中输入时,数字都会再次重置为 1
谁能帮帮我,万分感谢
以下可能是实现所需 objective 的一种可能解决方案:
代码段
// helper method to get either YYYY-MM (if full=false) or YYYY-MM-DD (otherwise)
const getDtYMD = (dt, full = false) => (
full ? dt.createdAt.split('T')[0] : dt.slice(0, 7)
);
// transform the arr to add a group-id (idGroup)
const sortAndGroup = arr => (
Object.values( // get the values of the result object
[...arr.map(x => ({...x}))].sort( // first sort the input-array reverse-chronological order (latest date at 0)
(a, b) => (getDtYMD(a, true) > getDtYMD(b, true) ? 1 : -1)
).reduce( // use .reduce to iterate over the sorted array
(acc, {createdAt : c, name}) => ({
...acc, // safe-keep existing accumulator object
[getDtYMD(c)]: ([
...(acc[getDtYMD(c)] || []),
{ // append new object to value array for key YYYY-MM
idGroup: (acc[getDtYMD(c)]?.length || 0) + 1,
createdAt: c, // update 'idGroup' based on existing value-array's length
name
}
])
}),
{}
)
).flat().sort(
(a, b) => (
getDtYMD(a, true) > getDtYMD(b, true)
? -1
: getDtYMD(a, true) < getDtYMD(b, true)
? 1
: a.idGroup > b.idGroup ? -1 : 1
)
)
);
const unsortedRawDataArray = [
{'createdAt': "2022-03-12T13:15:41.844+00:00", "name": '1 adipiscing'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '1 consectetur'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '2 consectetur'},
{'createdAt': "2022-03-04T11:56:27.545+00:00", "name": '3 consectetur'},
{'createdAt': "2021-01-15T09:12:25.418+00:00", "name": '1 amet'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '1 sit'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '2 sit'},
{'createdAt': "2021-01-09T09:12:25.418+00:00", "name": '3 sit'},
{'createdAt': "2021-01-02T08:52:55.418+00:00", "name": '1 dolor'},
{'createdAt': "2021-01-02T08:52:55.418+00:00", "name": '2 dolor'},
{'createdAt': "2021-12-15T09:12:25.418+00:00", "name": '1 ipsum'},
{'createdAt': "2021-12-11T08:52:55.418+00:00", "name": '1 lorem'},
];
console.log('sorted-grouped: ', sortAndGroup(unsortedRawDataArray));
console.log('original-array: ', unsortedRawDataArray);
说明
步骤说明已作为内联注释添加到上述代码片段中。
编辑
when there is data that has the same date/day the order returns to 1 again,
数组中的数据已更新为具有相同日期的多个数据,并且 idGroup
显示为未重置。
besides is there a way to reverse the number without changing the index, for example from 1-3 to 3-1.
这已在更新的代码段中完成。在 .flat()
之后,我们添加另一个 .sort()
,它不仅考虑日期,还考虑 idGroup
,以使数字从 3-1(反向)开始。
编辑 2
why after calling the sortAndGroup() function the data from unsortedRawDataArray also changes
此问题已修复。传入的arr
(数组)是通过.sort
直接变异的,最新版本的答案复制了arr
数组。