如何将以下数组分组并将其输出在EJS中?

发布于 2025-01-18 13:46:21 字数 606 浏览 0 评论 0原文

这是数组:

const emails = [
  { email: 'w@w', createdAt: 2022-03-31T17:07:36.675+00:00 },
  { email: 'a@a', createdAt: 2022-03-31T17:07:36.675+00:00 },
  { email: 'w@b', createdAt: 2022-04-31T17:07:36.675+00:00 },
  { email: 'w@c', createdAt: 2022-04-31T17:07:36.675+00:00 },
  { email: 'w@d', createdAt: 2022-06-31T17:07:36.675+00:00 },
]

我想在 ejs 中将其格式化为这样:

<div class='card'>
  <h3>Unique Date</h3>
  <div class='emails'>
    <p>All the emails of that unique date</p>
  </div>
</div>

我该怎么做?

This is the array:

const emails = [
  { email: 'w@w', createdAt: 2022-03-31T17:07:36.675+00:00 },
  { email: 'a@a', createdAt: 2022-03-31T17:07:36.675+00:00 },
  { email: 'w@b', createdAt: 2022-04-31T17:07:36.675+00:00 },
  { email: 'w@c', createdAt: 2022-04-31T17:07:36.675+00:00 },
  { email: 'w@d', createdAt: 2022-06-31T17:07:36.675+00:00 },
]

I want to format it like this in ejs:

<div class='card'>
  <h3>Unique Date</h3>
  <div class='emails'>
    <p>All the emails of that unique date</p>
  </div>
</div>

How can I do that?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

时光倒影 2025-01-25 13:46:21

在后端,您将需要使用reduce 和map 对现有数组进行分组以执行以下操作。

// Group emails by date
const groupedEmails = emails.reduce((acc, curr) => {
    const date = curr.createdAt.split('T')[0];
    if (!acc[date]) {
        acc[date] = [];
    }
    acc[date].push(curr);
    return acc;
}, {});

// Loop through grouped emails
const groupedEmailsArray = Object.keys(groupedEmails).map(key => {
    return {
        date: key,
        // Sort emails by email in alphabetical order
        emails: groupedEmails[key].sort((a, b) => {
            if (a.email < b.email) {
                return -1;
            }
            if (a.email > b.email) {
                return 1;
            }
            return 0;
        })
    }
});

然后,您需要将groupedEmailArray传递到您的视图并像这样渲染视图

<div class='card'>
  <h3>Unique Date</h3>
  <div class='emails'>
    <p>All the emails of that unique date</p>
    <% for(let group of groupedEmailsArray) { %>
      <p>
        <%= group.date %>
        <% for(let item of group.emails) { %>
            <%= item.email %>
        <% } %>
      </p>

      <% } %>
  </div>
</div>

这应该会产生您正在寻找的结果。

In your back end you will want to use reduce and map to group your existing array to do the following.

// Group emails by date
const groupedEmails = emails.reduce((acc, curr) => {
    const date = curr.createdAt.split('T')[0];
    if (!acc[date]) {
        acc[date] = [];
    }
    acc[date].push(curr);
    return acc;
}, {});

// Loop through grouped emails
const groupedEmailsArray = Object.keys(groupedEmails).map(key => {
    return {
        date: key,
        // Sort emails by email in alphabetical order
        emails: groupedEmails[key].sort((a, b) => {
            if (a.email < b.email) {
                return -1;
            }
            if (a.email > b.email) {
                return 1;
            }
            return 0;
        })
    }
});

You will then want to pass groupedEmailArray to your view and render the view like this

<div class='card'>
  <h3>Unique Date</h3>
  <div class='emails'>
    <p>All the emails of that unique date</p>
    <% for(let group of groupedEmailsArray) { %>
      <p>
        <%= group.date %>
        <% for(let item of group.emails) { %>
            <%= item.email %>
        <% } %>
      </p>

      <% } %>
  </div>
</div>

This should yield the result you are looking for.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文