tree
tree

Reputation: 687

Using a variable as the index of an array in jade

I'm trying to access an array using a variable as the index and then output it like so:

h3= users[{#id}].first_name

But I get a "SyntaxError: Unexpected token ILLEGAL" because of the #{id}. What is the correct way to do this?

Upvotes: 0

Views: 2185

Answers (1)

zemirco
zemirco

Reputation: 16395

You can just use id without hash or curly braces.

index.js

exports.index = function(req, res){
  res.render('index', { 
    title: 'Express',
    users: [{first_name: 'John', age: 20}, {first_name: 'Mike', age: 30}],
    id: 1
  });
};

index.jade

extends layout

block content
  h1= title
  p Welcome to #{title}
  p= users[id].first_name

Upvotes: 3

Related Questions