Home > Software design >  How to display an array of object in ejs with conditions
How to display an array of object in ejs with conditions

Time:10-29

i'm doing a web app to manage my money with node Js.

Here is what it's look like : enter image description here

I've done almost everything, but I still have one part to do, which is to display my last 30 transactions. I can't display my transactions as I want. It is necessary that if the date of the current transaction is different from the transaction just before then it displays the date and the transaction otherwise just the transaction. I block at the condition level if the date is different

At first I tried to display the first transaction with the date and then use (for) or (for in) to display the others with the condition but i can't use the i variable in my array of objects.

like this :

<%= transaction30[0].DateT%>

<%= transaction30[0].Name%>

<%= transaction30[0].Montant%>

<% for var i in transactions30 %> or <% for var i=1; i<= transactions30.length;i %> {

<% if(transactions30[i].DateT != transactions30[i-1].DateT) %>

then display date and infos

<% }else{ %>

juste display infos

<% } %>

Then I read somewhere that you shouldn't use (for) for arrays of objects. So I tried a forEach. Here is my code :

                    <h1>
                        <%= transactions30[0].DateT %>
                    </h1>


                    <% let status=transactions30[0].DateT %>
                        <% transactions30.forEach(function(item){ %>
                            <% console.log(item.DateT," et ",status) %>
                                    <% if(item.DateT!=status) {%>
                                        <h1>
                                            <%= item.DateT %>
                                        </h1>
                                        <div >
                                <div ></div>
                                <div >
                                    <p >
                                        <%= item.Nom %>
                                    </p>
                                    <p >
                                        <%= item.Categorie %>
                                    </p>
                                </div>
                                <p >
                                    <%= item.Montant %>
                                </p>
                </div>
                <% status=item.DateT %>
                    <% }else{ %>
                        <div >
                            <div ></div>
                            <div >
                                <p >
                                    <%= item.Nom %>
                                </p>
                                <p >
                                    <%= item.Categorie %>
                                </p>
                            </div>
                            <p >
                                <%= item.Montant %>
                            </p>
                        </div>
                        <% } %>
                            <% }); %>

The 'prevdate' variable is updating well, but it always display date of all transactions. The (if) condition work but only for the first transaction because i dont get the date of the first transaction two times ( i display the first date at start, out of the forEach condition)

Here is the array for better comprehension : array named transactions30

Can someone help me please ?

CodePudding user response:

If found the solution. Instead of (if date != previsousdate) i had to do if(date < previousdate || date > previousdate) and now it's work

<h1>
                        <%= transactions30[0].DateT %>
                    </h1>


                    <% let status=transactions30[0].DateT %>
                        <% transactions30.forEach(function(item){ %>
                            <% console.log(item.DateT," et ",status) %>
                                    <% if(item.DateT>status || item.DateT < status) {%>
                                        <h1>
                                            <%= item.DateT %>
                                        </h1>
                                        <div >
                                <div ></div>
                                <div >
                                    <p >
                                        <%= item.Nom %>
                                    </p>
                                    <p >
                                        <%= item.Categorie %>
                                    </p>
                                </div>
                                <p >
                                    <%= item.Montant %>
                                </p>
                </div>
                <% status=item.DateT %>
                    <% }else{ %>
                        <div >
                            <div ></div>
                            <div >
                                <p >
                                    <%= item.Nom %>
                                </p>
                                <p >
                                    <%= item.Categorie %>
                                </p>
                            </div>
                            <p >
                                <%= item.Montant %>
                            </p>
                        </div>
                        <% } %>
                            <% }); %>

  • Related