Displaying elements of array in Pug template is a key task for

As a newcomer to the Jade/Pug template engine used in Express, I am faced with a challenge. I need to display the name property of each object within an associative array that is passed as a parameter to my pug template from an express route module.

I have attempted various methods, such as:

  each element in listOfElements
      p #{element.name}

where listOfElements is the parameter name being passed

Unfortunately, I have not achieved the desired outcome so far.

UPDATE

Currently, I am referring to the documentation which only provides an example using UL tags (not suitable for my case).

Following the documentation's guidelines, I have tried the following approach:

ul
each element in listOfElements
    li = element.name

However, the rendered page displays a list where each bullet point contains the text " = element.name"

Answer №1

According to the information provided on the Pug website regarding iterations, you can receive buffered code by adding an = immediately after the tag name. The necessary documentation is available here. Therefore, in your second try, the following code will function correctly:

ul
each element in listOfElements
    li= element.name

On the other hand, for the first attempt, I experimented with the subsequent code and it generated the anticipated result (as displayed below the snippet):

ul
  each val in [{1:'a'}, {1:2}, {1:3}, {1:4}, {1:5}]
    li #{val[1]}

This produces:

<ul>
  <li>a</li>
  <li>2</li>
  <li>3</li>
  <li>4</li>
  <li>5</li>
</ul>

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Triggering jQuery Submit Form when Form is Modified

I need help with automatically submitting a form using jQuery when an input changes. The specific input I am working with is a date picker, and I want the form to be submitted as soon as a user makes a selection. <form id="select_date" name="select_da ...

Triggering JavaScript events using jQuery

I am experiencing an issue with an input element that triggers a modal containing a table when clicked. From this table, you can select a line and a JavaScript function modifies the value of the input element accordingly. However, I am trying to detect the ...

Error: Trying to use 'search' before it has been initialized causes a ReferenceError

Every time I run my code, I encounter this reference error but I can't figure out what's causing it. ReferenceError: Cannot access 'search' before initialization App C:/Users/GS66/Desktop/IN20/IFN666/week4/src/App.js:60 57 | 58 | expor ...

Vue - Additional loading may be required to manage the output of these loaders

Currently working with Vue and babel. I have a function that's been exported // Inside file a.js export async function get() { ... } I am trying to link this exported function to a static method of MyClass // Inside file b.js import myInterface fr ...

Error message "invalid function call this.job.execute" when using Node.js node-schedule npm package

Having just started with nodejs, I created a nodejs program and set it to run every minute using the node-schedule library. However, after running for some time and producing several logs in the console, I encountered an error stating that this.job.execute ...

What is the procedure for inserting a {key: value} object into an array in MongoDB?

I've been trying to update my User objects by adding a post to the array I created for them. Despite several attempts, nothing seems to be working and the user object remains unchanged. MongoDB is new to me, but I'm really enjoying using it and l ...

using Javascript to eliminate necessary tags from concealed tabs

My goal is to dynamically remove the required tag from fields in hidden tabs when a tab is clicked using JavaScript. The presence of the required tag on unused fields causes issues with form submission, preventing data insertion into the database. Here&apo ...

Using JQuery to dynamically change className based on specific logic conditions

I am struggling to dynamically change the class name of a div based on the text inside another div using jQuery. Here is an example of the HTML structure: <div class="div-overlay"> <a class="image" href="https://www.e ...

Issues with ontimeupdate event not triggering in Chrome for HTML5 audio files

After creating an HTML5 audio element and setting a listener for when its time updates, I have run into an issue where the ontimeupdate function does not fire in Chrome, including Chrome on Android. The audio plays without any issues in other browsers. va ...

When you hover over HTML tables, they dynamically rearrange or shift positions

Issue: I am encountering a problem with multiple tables where, upon hovering over a row, the tables are floating rather than remaining fixed in place. Additionally, when hovering over rows in the first or second table, the other tables start rendering unex ...

Tips on extracting variables from JMeter Selenium WebDriver

Currently, I am dealing with a token that is being returned in the body of a web page. WDS.browser.findElement(org.openqa.selenium.By.xpath("//*[contains(@name,'RequestVerificationToken')]")).getText(); This token is what I need to extract: ...

Transferring Data from NodeJS to client-side JavaScript File

Is there a way to transfer a server-side JavaScript variable to a <script> tag within an HTML view? In the route file, I have: exports.index = function(req, res){ res.sendfile('views/index.html', { data: {foo: bar} }); }; If I ...

To view the previous or next image, simply click on the links and watch as the new image fades in seamlessly while maintaining

Whenever I click on a prev/next link, a specific div loads different images that loop. The JavaScript successfully changes the image source when the prev or next button is clicked. It works flawlessly. However, I am facing an issue. I want each new image ...

Reviewing for the presence of "Undefined" in the conditional statement within Transpiled Javascript code for

While perusing through some transpiled Angular code, I came across this snippet: var __decorate = (undefined && undefined.__decorate) || function (decorators, target, key, desc) { I'm puzzled by the usage of undefined in this context. Can an ...

Unlimited Possibilities in Designing Shared React Components

Seeking the most effective strategies for empowering developers to customize elements within my React shared component. For example, I have a dropdown and want developers to choose from predefined themes that allow them to define highlight color, font siz ...

PassportJS OAuth2Strategy exhibits unexpected behavior by returning a 400 error code instead of performing a redirect during

Currently, I am in the process of setting up Discord OAuth2 PKCE using Passport.js and the passport-oauth2 const discordStrategy = new OAuth2Strategy({ authorizationURL: 'https://discord.com/api/oauth2/authorize', tokenURL: 'https://disc ...

Activate Click, or Pop-up Box

I've been attempting to log in to the Udemy site using JavaScript, but I'm having trouble triggering the click action on the "log in" link. Unfortunately, the .click() method doesn't seem to be working when I try to select the element. The l ...

Issue with Angular controller failing to load when link is clicked

I am currently developing a webpage that incorporates Angular and responds to ajax requests by populating data into a table. When I manually enter the address, everything works as expected. However, if I navigate to the page using a link ( <a href="/pro ...

A guide on displaying a JSON object using the ng-repeat directive

Looking to create a dynamic treeview menu with angularJS? Wondering how to achieve the desired results using a controller ($scope.results) and JSON data? Check out the code snippet below for an example of how to structure your treeview: <ul> < ...

Accessing composable variables in Vue 3 without having to redefine refs from within a function

Currently, I am implementing a parent companyList component along with a reusable Table component and an useFetch composable in vue 3.2. Prior to integrating the Table component, my code looked like this: companyList <script setup> import { com ...