Javascript 如何在EJS模板中显示此内容?

Javascript 如何在EJS模板中显示此内容?,javascript,ejs,Javascript,Ejs,我制作了一个页面,从数据库中获取一些文章。在下划线和u.groupBy的帮助下,我按年份(数据库中的一个字段)对它们进行了分组,现在它们是这样分组的 { '2001': [ { _id: 5e286db8a47af74400f4a4f7, title: 'Trying to connect to SSH, Permision Denied (public key)', typeofdisaster: 'Tsunami', year: '2001'

我制作了一个页面,从数据库中获取一些文章。在下划线和u.groupBy的帮助下,我按年份(数据库中的一个字段)对它们进行了分组,现在它们是这样分组的

 { '2001':
   [ { _id: 5e286db8a47af74400f4a4f7,
       title: 'Trying to connect to SSH, Permision Denied (public key)',
       typeofdisaster: 'Tsunami',
       year: '2001',
       month: 'December',
       day: '08',
       description: 'ADFASDFASD',
       aditional: 'fdasfas',
       image1:
        'images/2020-01-22T15:43:52.720Z-67287305_1471150326359475_6265289848051990528_o.jpg',
       image2: 'none',
       location: 'Scenic Highhwat 23412',
       postid: 'GyPUk',
       datePosted: '1579707832851',
       __v: 0 } ],
  '2009':
   [ { _id: 5e285af6f4caf441cc9b451f,
       title: 'This is an article',
       typeofdisaster: 'Tsunami',
       year: '2009',
       month: 'March',
       day: '5',
       description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T14:23:50.840Z-67287305_1471150326359475_6265289848051990528_o (1).jpg',
       image2: 'none',
       location: '245 Scenic Highway Lawrenceville, GA',
       postid: 'v6Lih',
       datePosted: '1579703030971',
       __v: 0 },
     { _id: 5e286a5ffeadf743747f7ff6,
       title: 'This is a post',
       typeofdisaster: 'Tornado',
       year: '2009',
       month: 'December',
       day: '08',
       description: 'LOREM IPSUM',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T15:29:34.965Z-82668748_468933630458447_4082761341385834496_n.jpg',
       image2: 'none',
       location: 'Vasile Alecsandri 16, Campia Turzii, Cluj',
       postid: 'r39GP',
       datePosted: '1579706975104',
       __v: 0 } ] }
<% array.forEach((value,key)) { %>

    <h3><%= key%></h3>

    <% for(let elem of value) { %>

    <span><%= elem.title%></span>

    <%}%>

<%}%>
一切都好。它们可以很好地分组,但是如何在EJS文件中呈现它们呢? 我这样做是因为我想做一个像这样按年份排序的帖子档案

 2009 
    Post Title
    Post Title 2 
 2005
    Post Title
 2001 
    Post title 
    Post title 2
    Post title 3
这是呈现该页面的函数

exports.getIndex = async (req,res,next) => {

    const authenticated = req.session.isLoggedIn;
    let postLists = [];
      let posts = await Post.find();
      for(let i = 0; i< posts.length;i++) {
        postLists.push(posts[i]);
      }

     console.log(_.groupBy(postLists, 'year'));

res.render('index', {
      pageTitle:'DISASTER DATABASE |  HOME',
      path:'/',
      authenticated: authenticated,
      user: req.session.user,
      posts: _.groupBy(postLists,'year')
    });
exports.getIndex=async(请求、恢复、下一步)=>{
const authenticated=req.session.isLoggedIn;
让postLists=[];
let posts=等待Post.find();
for(设i=0;i
我在前端非常糟糕,我不知道如何在EJS文件中实现这件事,我的意思是我得到了我需要的所有数据,但我不知道如何显示它

<%- include('./includes/head.ejs'); %>
<main>
 <%- include('./includes/navigation.ejs'); %>

 <div class="split c-archivecontainer">
  <h5 style=" color: #5D4954;
  text-transform: uppercase;
  letter-spacing: 5px;
  font-size:15px;
  font-family: 'Poppins', sans-serif;">Disasters archive</h5>
 </div>
 <div class="split c-mapcontainer">
        <h5 style=" color: #5D4954;
        text-transform: uppercase;
        letter-spacing: 5px;
        font-size:15px;
        font-family: 'Poppins', sans-serif;">Disasters Map</h5>
 </div>

 <script>


 </script>
</main>
<%- include('./includes/end.ejs'); %>

灾害档案
灾害地图
你能帮我实现这一点吗?我的意思是如何在所有年份中循环显示年份,然后是分配给该年份的职位。提前谢谢

忽略第二个div
我这样做只是为了拆分页面,在第一部分中,我想把归档文件放进去

您可以这样编写代码

 { '2001':
   [ { _id: 5e286db8a47af74400f4a4f7,
       title: 'Trying to connect to SSH, Permision Denied (public key)',
       typeofdisaster: 'Tsunami',
       year: '2001',
       month: 'December',
       day: '08',
       description: 'ADFASDFASD',
       aditional: 'fdasfas',
       image1:
        'images/2020-01-22T15:43:52.720Z-67287305_1471150326359475_6265289848051990528_o.jpg',
       image2: 'none',
       location: 'Scenic Highhwat 23412',
       postid: 'GyPUk',
       datePosted: '1579707832851',
       __v: 0 } ],
  '2009':
   [ { _id: 5e285af6f4caf441cc9b451f,
       title: 'This is an article',
       typeofdisaster: 'Tsunami',
       year: '2009',
       month: 'March',
       day: '5',
       description:
        'Lorem Ipsum is simply dummy text of the printing and typesetting industry. Lorem Ipsum has been the industry\'s standard dummy text ever since the 1500s, when an unknown printer took a galley of type and scrambled it to make a type specimen book. It has survived not only five centuries, but also the leap into electronic typesetting, remaining essentially unchanged. It was popularised in the 1960s with the release of Letraset sheets containing Lorem Ipsum passages, and more recently with desktop publishing software like Aldus PageMaker including versions of Lorem Ipsum.',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T14:23:50.840Z-67287305_1471150326359475_6265289848051990528_o (1).jpg',
       image2: 'none',
       location: '245 Scenic Highway Lawrenceville, GA',
       postid: 'v6Lih',
       datePosted: '1579703030971',
       __v: 0 },
     { _id: 5e286a5ffeadf743747f7ff6,
       title: 'This is a post',
       typeofdisaster: 'Tornado',
       year: '2009',
       month: 'December',
       day: '08',
       description: 'LOREM IPSUM',
       aditional: 'https://www.google.com',
       image1:
        'images/2020-01-22T15:29:34.965Z-82668748_468933630458447_4082761341385834496_n.jpg',
       image2: 'none',
       location: 'Vasile Alecsandri 16, Campia Turzii, Cluj',
       postid: 'r39GP',
       datePosted: '1579706975104',
       __v: 0 } ] }
<% array.forEach((value,key)) { %>

    <h3><%= key%></h3>

    <% for(let elem of value) { %>

    <span><%= elem.title%></span>

    <%}%>

<%}%>

在这里,您可以在原始数组上运行forEach循环,从而访问每个元素的键和值

然后,您可以通过每个元素(也是一个循环)的值再次打印元素和循环的键

在第二个循环中,您可以打印每篇文章的标题