此问题在此处已有答案:
group array with timestamps(2个答案)
2天前关闭.
const transactions = [
{
date: "2021-04-16T08:52:24.408Z",
type: "cash withdrawal",
image: "",
amount: "-5000",
},
{
date: "2021-04-14T19:30:00.000Z",
type: "Cash Received",
image: "",
amount: "5000",
},
{
date: "2021-04-14T19:30:00.000Z",
type: "Cash Received",
image: "",
amount: "5000",
},
{
date: "2021-04-14T19:30:00.000Z",
type: "Cash Received",
image: "",
amount: "5000",
},
{
date: "2021-03-17T20:30:00.000Z",
type: "Cash Received",
image: "",
amount: "5000",
},
{
date: "2021-02-01T20:30:00.000Z",
type: "cashwithdraw",
image: "",
amount: "-5000",
},
{
date: "2021-01-06T20:30:00.000Z",
type: "cashwithdraw",
image: "",
amount: "-5000",
},
{
date: "2020-08-06T19:30:00.000Z",
type: "Cash Received",
image: "",
amount: "5000",
},
];
给定上面的对象数组,我希望以这样一种方式进行排序,即假设1月10日创建的所有事务都在1月10日之下
这是我一直在努力的东西,但是我不能准确的把它设置成显示屏。
let resp = transactions.reduce((acc, item) => {
if (!acc[item.date])
acc[item.date] = {
date: item.date,
data: [],
};
acc[item.date].data.push(item);
console.log(acc?.data?.date);
return acc
}, {});
这是我期望实现的enter image description here
2条答案
按热度按时间9rnv2umw1#
你需要写上日期而不是完整的日期。
ru9i0ody2#
不清楚您希望最终结果如何,但这将起作用: