2017-12-27 470 views
1

如何将价格数组添加到Jade中的第二个'td'标签?我希望它是迭代。可能吗?Jade迭代到HTML表格

- var item = ['Item1', 'Item2', 'Item3'] 
- var price = ['40', '90', '140'] 

table.pricetable 
    thead 
     tr 
      th item 
      th price 
    tbody 
     each a in item 
      tr 
       td #{a} 
       td ??? 

感谢, 西蒙

回答

1

假设他们有直接关系:

- var item = ['Item1', 'Item2', 'Item3'] 
- var price = ['40', '90', '140'] 

table.pricetable 
    thead 
     tr 
      th item 
      th price 
    tbody 
     each a, index in item 
      tr 
       td #{a} 
       td #{price[index]} 

然而,更好的方法是使用对象的数组,而不是两个单独的数组:

- var items = [{item: 'Item1', price: 40}, {item: 'Item2', price: 90}, {item: 'Item3', price: 140}] 

table.pricetable 
    thead 
     tr 
      th item 
      th price 
    tbody 
     each a in item 
      tr 
       td #{a.item} 
       td #{a.price} 
1

是的,这是可能的,通过也越来越在循环索引:

- var item = ['Item1', 'Item2', 'Item3'] 
- var price = ['40', '90', '140'] 

table.pricetable 
    thead 
     tr 
      th item 
      th price 
    tbody 
     each a, index in item 
      tr 
       td #{a} 
       td #{price[index]} 

这可以让你获得当前值的指数你”重复迭代,并可用于访问另一个数组中的相同位置。

+0

你2分钟打我哈哈 –