2017-12-27 14 views
1

Wie kann ich das Preis-Array zum zweiten 'td' Tag in Jade hinzufügen? Ich möchte, dass es eine Iteration ist. Ist es möglich?Jade-Iteration zu HTML-Tabelle

- 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 ??? 

Danke, Simon

Antwort

1

Vorausgesetzt, dass sie in direktem Zusammenhang:

- 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]} 

jedoch ein viel besserer Ansatz ein Array von Objekten zu verwenden wäre, anstelle von zwei getrennten Arrays:

- 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

Ja, das ist möglich, indem auch der Index in der Schleife bekommen:

- 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]} 

Auf diese Weise können Sie den Index des aktuellen Wertes erhalten Sie‘ re iteriert über und kann verwendet werden, um auf die gleiche Position in dem anderen Array zuzugreifen.

+0

Sie mich schlagen 2 Minuten Haha –

Verwandte Themen