U
U
Uncle Tolya2020-10-08 05:04:46
JavaScript
Uncle Tolya, 2020-10-08 05:04:46

How to pull data from JSON to PUG using a loop?

I still don't fully understand how to use the loop in pug.
I have JSON:

"nav": [
    {
      "link": {
        "title": "banana",
        "href": "/banana"
      }
    },
    {
      "link": {
        "title": "tomato",
        "href": "/tomato"
      }
    },
    {
      "link": {
        "title": "mango",
        "href": "/mango"
      }
    }
  ]

I wanted something like this to work.
ul
    each entry in nav.link
      li: a(href= entry.href)= entry.title


if it is possible of course.
maybe I wrote something utter, but how much I googled - I did not figure it out.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
P
profesor08, 2020-10-08
@profesor08

I still don't fully understand how to use the loop in pug.

It's not pug, it's a misunderstanding of javascript objects and arrays.
-
  const data = {
    "nav": [
      {
        "link": {
          "title": "banana",
          "href": "/banana"
        }
      },
      {
        "link": {
          "title": "tomato",
          "href": "/tomato"
        }
      },
      {
        "link": {
          "title": "mango",
          "href": "/mango"
        }
      }
    ]
  };

each item in data.nav
  li: a(href=item.link.href) !{item.link.title}

A
Alexander, 2020-10-08
@Dgacarda

https://pugjs.org/language/iteration.html
The very first example

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question