简体   繁体   English

node js express 与 ejs forEach object 并放入表中

[英]node js express with ejs forEach object and put into table

Hello i have just started to learn node js today and i'm trying to forEach an object and place into a table.您好,我今天刚开始学习 node js,我正在尝试为每个 object 放置一个表格。

In my router:在我的路由器中:

var obj = JSON.parse(`[{
    "Name": "ArrowTower",
    "Class": "ArrowTower",
    "GoldCosts": [0, 100, 200, 600, 1200, 2000, 8000, 35000],
    "WoodCosts": [5, 25, 30, 40, 50, 70, 300, 800],
    "StoneCosts": [5, 20, 30, 40, 60, 80, 300, 800],
    "TokenCosts": [0, 0, 0, 0, 0, 0, 0, 0],
    "TowerRadius": [600, 650, 700, 750, 800, 850, 900, 1000],
    "MsBetweenFires": [400, 333, 285, 250, 250, 250, 250, 250],
    "Height": 95.99,
    "Width": 95.99,
    "Health": [150, 200, 400, 800, 1200, 1600, 2200, 3600],
    "MsBeforeRegen": [10000, 10000, 10000, 10000, 10000, 10000, 10000, 10000],
    "HealthRegenPerSecond": [2, 5, 10, 20, 40, 80, 110, 150],
    "DamageToZombies": [20, 40, 70, 120, 180, 250, 400, 500],
    "DamageToPlayers": [5, 5, 5, 5, 5, 5, 6, 6],
    "DamageToPets": [5, 5, 5, 5, 5, 5, 6, 6],
    "DamageToNeutrals": [250, 350, 450, 550, 650, 750, 850, 1000],
    "ProjectileLifetime": [1300, 1300, 1300, 1300, 1300, 1300, 1300, 1300],
    "ProjectileVelocity": [60, 65, 70, 70, 75, 80, 120, 140],
    "ProjectileName": "ArrowProjectile",
    "ProjectileAoe": [false, false, false, false, false, false, false, false],
    "ProjectileCollisionRadius": [10, 10, 10, 10, 10, 10, 10, 10]
}]`)

router.get('/info', (req, res) => {
  res.render("info", { obj: obj });
});

In my ejs file i have tried:在我的 ejs 文件中,我尝试过:

<table id="table">
  <tr>
    <td>Tier</td>
    <td>Velocity</td>
    <td>Reload</td>
    <td>Damage</td>
    <td>Health regen/s</td>
  </tr>
  
  <% Object.values(obj).forEach(function(tower){%>
  <tr>
    <td>Tier </td>
    <td><%= tower.ProjectileVelocity %></td>
    <td><%= tower.MsBetweenFires %></td>
    <td><%= tower.DamageToZombies %></td>
    <td><%= tower.HealthRegenPerSecond %></td>
  </tr>
  <%})%>
</table>

The output for above try: image for output above上述 output 尝试:上述 output 的图像

I have also tried:我也试过:

 <% Object.values(obj).forEach(function(tower){%>
  <tr>
    <td>Tier </td>
    <td><%= tower['ProjectileVelocity'][0] %></td>
    <td><%= tower['MsBetweenFires'][0] %></td>
    <td><%= tower['DamageToZombies'][0] %></td>
    <td><%= tower['HealthRegenPerSecond'][0] %></td>
  </tr>
 <%})%>

The output for above try: image for output above上述 output 尝试:上述 output 的图像

What i'm trying to achieve: What i would like to achieve image我想要实现的目标:我想要实现的图像

I just can't seem to work out how to get it like the image above or if it can even be done.我似乎无法弄清楚如何像上面的图片那样获得它,或者它是否可以完成。

You have to add an extra loop for each row.您必须为每一行添加一个额外的循环。 If you are sure that all array properties have the same length, you can use a for or a forEach loop according to an array length (eg ProjectileVelocity array length).如果您确定所有数组属性的长度相同,则可以根据数组长度(例如 ProjectileVelocity 数组长度)使用forforEach循环。

    <table id="table">
        <tr>
            <td>Tier</td>
            <td>Velocity</td>
            <td>Reload</td>
            <td>Damage</td>
            <td>Health regen/s</td>
        </tr>
                                
        <% Object.values(obj).forEach(function(tower){%>
            <% for (let i = 0; i < tower.ProjectileVelocity.length; i++) { %>
                <tr>
                    <td>Tier </td>
                    <td><%= tower.ProjectileVelocity[i] %></td>
                    <td><%= tower.MsBetweenFires[i] %>ms</td>
                    <td><%= tower.DamageToZombies[i] %></td>
                    <td><%= tower.HealthRegenPerSecond[i] %></td>
                </tr>
            <%  } %>
        <%})%>
   </table>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM