Daimz
Daimz

Reputation: 3281

How can I do an if last item in jade

As you can see from below I have an array, I would like to remove the svg from the last item in that array when it runs. How might I do this with a condition? Something like if last:item else add svg

-navlinks = {"Home":"/Home", "About":"/About", "Store Directory":"/Store-Directory", "Store Page":"/Store-Page", "Events":"/Events",}
ul.navbar-menu
  for val, key in navlinks
    li 
      a(href='#{val}') #{key}
      svg.icon.icon-dots
        use(xlink:href="#icon-dots")

Upvotes: 2

Views: 3642

Answers (1)

Waldo Jeffers
Waldo Jeffers

Reputation: 2279

Well the thing is, contrary to what you said, navlinks is not an Array, but rather an Object. Since Object elements do not have a numeric index, the notion of last does not have much meaning.

However, you could iterate over Object.keys(navlinks) which is a proper Array with a numeric index. So you could do something like :

-navlinks = {"Home":"/Home", "About":"/About", "Store Directory":"/Store-Directory", "Store Page":"/Store-Page", "Events":"/Events",}
ul.navbar-menu
  - each key, index in Object.keys(navlinks)
     li 
        a(href='#{val}')= navlinks[key]
        if index < Object.keys(navlinks).length - 1
          svg.icon.icon-dots
             use(xlink:href="#icon-dots")

Upvotes: 9

Related Questions