Ricerca…


introduzione

Come eseguire iterazioni su un oggetto JSON semplice e risparmiare un sacco di digitazione

Osservazioni

Devi aver installato Node.js e Pug

Ogni iterazione

Crea app.js con un semplice archivio dati :

app.get("/bookstore", function (req, res, next) {
    // Your route data
   var bookStore = [
        {
            title: "Templating with Pug",
            author: "Winston Smith",
            pages: 143,
            year: 2017        
        },
        {
            title: "Node.js will help",
            author: "Guy Fake",
            pages: 879,
            year: 2015        
        }
    ];
    res.render("index", {
        bookStore: bookStore
    });
});

index.pug un index.pug dati utilizzando un file index.pug e un loop:

each book in bookStore
  ul
    li= book.title
    li= book.author
    li= book.pages
    li= book.year

Il risultato sarà:

<ul>
  <li>Templating with Pug</li>
  <li>Winston Smith</li>
  <li>143</li>
  <li>2017</li>
</ul>
<ul>
  <li>Node.js will help</li>
  <li>Guy Fake</li>
  <li>879</li>
  <li>2015</li>
</ul>

Riferimento



Modified text is an extract of the original Stack Overflow Documentation
Autorizzato sotto CC BY-SA 3.0
Non affiliato con Stack Overflow