0

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?

tree
  • 657
  • 1
  • 7
  • 11

1 Answers1

3

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
zemirco
  • 16,171
  • 8
  • 62
  • 96
  • @zemirco Quite old topic, but how to access `users[id]` if you're on the client js side `script(type='text/javascript').`, like it's [described here](https://stackoverflow.com/questions/10919650/accessing-express-js-local-variables-in-client-side-javascript)?
    I tried using `!{users}[id]` or `users[id]`, but it's giving me "false true undefined" as result.
    – Tomblarom Oct 18 '19 at 08:04