2017-12-27 472 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分鐘打我哈哈 –