reactjs 如何按创建日期对事务列表进行分组[重复]

zzlelutf  于 2022-12-18  发布在  React
关注(0)|答案(2)|浏览(190)

此问题在此处已有答案

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

9rnv2umw

9rnv2umw1#

你需要写上日期而不是完整的日期。

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" }],
    result = transactions.reduce((acc, item) => {
        const date = item.date.slice(0, 10);
        (acc[date] ??= { date, data: [] }).data.push(item);
        return acc;
    }, {});

console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }
ru9i0ody

ru9i0ody2#

不清楚您希望最终结果如何,但这将起作用:

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",
  },
];

const res = transactions.reduce((acc, item) => {
  const date = item.date.split("T")[0];
  acc[date] ??= [];
  acc[date].push(item);
  return acc;
}, {});

console.log(res);

相关问题