0

我有以下数组:

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"
    });
});

为什么?

4

1 回答 1

1

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

于 2013-01-15T00:32:40.137 回答