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?

1 Answer 1

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
Sign up to request clarification or add additional context in comments.

1 Comment

@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? <br> I tried using !{users}[id] or users[id], but it's giving me "false true undefined" as result.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.