Array.select不选择元素

时间:2013-01-15 00:23:49

标签: javascript express

我有以下数组:

var jobs = [
  { ID: 'grt34hggf', Title: 'tobi', Location: 'Los Angeles, CA', Description: "bla-bla-bla", DateExpiration: "FFF" },
  { ID: 'vbnhty', Title: 'n', Location: 'Los Angeles, CA', Description: "bla-bla-bla", DateExpiration: "FFF" },
  { ID: 'bnm7ii78', Title: 'bnui', Location: 'Los Angeles, CA', Description: "bla-bla-bla", DateExpiration: "FFF" }
];

然后我尝试从这个数组中获取一个元素。以下代码不起作用:

app.get('/job/:id', function (req, res) {
    res.render('job', {
        job: jobs.filter(function(x) { return x.ID == 'bnm7ii78'; }),
        title: "Job Descr",
        header: "descr"
    });
});

但以下代码有效:

app.get('/job/:id', function (req, res) {
    res.render('job', {
        job : jobs[0],
        title: "Job Descr",
        header: "descr"
    });
});

为什么?

1 个答案:

答案 0 :(得分:1)

看起来您期望一个对象,但jobs.filter(...)会返回符合您条件的所有元素的array。您可以尝试jobs.filter(...)[0]