Daimz Daimz - 4 months ago 15
Jade Question

How to find out if only one item exists in array

I have a gallery and when there is only one item in the gallery I want to disable the thumbnails.

I am using a JADE loop to create the thumbnail tray like this:

.thumbnails.tumbnail-tray
each thumbnail in product.thumbnails
- var className = (thumbnail.selected) ? 'active' : ''
.thumb(class=className)
img(src="images/#{thumbnail.thumb}" main-image="images/#{thumbnail.src}" alt="thumbnail.alt")


and this is the JSON structure:

"thumbnails": [
{
"selected": true,
"src": "image1.jpg",
"thumb": "image1_thumb.jpg",
"alt": ""
}
]


What I would like to do is create an if
thumbnails.length => 2
display thumbnails tray
else
don't.

Any ideas how I might be able to do this?

pp_ pp_
Answer

You can assign your JSON array to a variable and then use the length property:

var thumbnails = [
  {
    "selected": true,
    "src": "image1.jpg",
    "thumb": "image1_thumb.jpg",
    "alt": ""
  }
]

if (thumbnails.length > 1) {
  // do something
}