2013-02-18 98 views
0

我試圖用一個變量作爲索引來訪問數組,然後輸出它像這樣的指標:使用變量作爲數組的玉

h3= users[{#id}].first_name

但我得到一個「的SyntaxError :意外的令牌非法「,因爲#{id}。什麼是正確的方法來做到這一點?

回答

2

您可以使用id而不使用散列或花括號。

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 
+0

那是愚蠢的,謝謝! – Smitha 2013-02-21 15:43:03