在Express API的javascript数组中引用子对象

时间:2022-07-07 00:18:16

I have this dummy data:

我有这个虚拟数据:

var data = [{
    articles : [{
        id : '0',
        url : 'foo',
        title : 'Foo',
        body : 'some foo bar',
        category : 'foo',
        tags : [
            'foo'
        ]
    }, {
        id : '1',
        url : 'foo-bar',
        title : 'Foo bar',
        body : 'more foo bar',
        category : 'foo',
        tags : [
            'foo', 'bar'
        ]
    }, {
        id : '2',
        url : 'foo-bar-baz',
        title : 'Foo bar baz',
        body : 'more foo bar baz',
        category : 'foo',
        tags : [
            'foo',
            'bar',
            'baz'
        ]
    }]
}, {
    users : [{
        name: 'Admin'
    }, {
        name: 'User'
    }]
}];

I have a router which I want to request only the matching article with it's ID from the articles:

我有一个路由器,我只想从文章中请求匹配的文章ID:

// Grab single article
// http://127.0.0.1:3000/api/0
router.get('/:id', function(req, res) {
  if(data.length <= req.params.id || req.params.id < 0) {
    res.statusCode = 404;
    return res.send('Error 404: No quote found');
  };
  var q = data[req.params.id];
  res.json(q);
});

Using the following URL http://127.0.0.1:3000/api/0 returning the articles group but not the first article.

使用以下URL http://127.0.0.1:3000/api/0返回文章组但不返回第一篇文章。

Thank You for your help!

感谢您的帮助!

2 个解决方案

#1


1  

if you really want to match the "id" and not just the index in the array, you should do something like this:

如果你真的想匹配“id”而不仅仅是数组中的索引,你应该这样做:

var articles = data[0].articles;
var q = articles.filter(function (article) {
   return article.id === req.params.id;
});

res.json(q[0]);

#2


1  

The 0th element in the outer array is the object that contains all the articles. First you'll need to drill down to the articles array: data[0].articles. Then to get just the object from the array with the id property that matches the id number you need, you could use the lo_dash library's _.find() function. https://lodash.com/docs#find

外部数组中的第0个元素是包含所有文章的对象。首先,您需要深入到文章数组:data [0] .articles。然后,为了从数组中获取具有与您需要的id号匹配的id属性的对象,您可以使用lo_dash库的_.find()函数。 https://lodash.com/docs#find

The call would look something like: _.find(data[0].articles, {id:req.params.id});

调用看起来像:_. find(data [0] .articles,{id:req.params.id});

#1


1  

if you really want to match the "id" and not just the index in the array, you should do something like this:

如果你真的想匹配“id”而不仅仅是数组中的索引,你应该这样做:

var articles = data[0].articles;
var q = articles.filter(function (article) {
   return article.id === req.params.id;
});

res.json(q[0]);

#2


1  

The 0th element in the outer array is the object that contains all the articles. First you'll need to drill down to the articles array: data[0].articles. Then to get just the object from the array with the id property that matches the id number you need, you could use the lo_dash library's _.find() function. https://lodash.com/docs#find

外部数组中的第0个元素是包含所有文章的对象。首先,您需要深入到文章数组:data [0] .articles。然后,为了从数组中获取具有与您需要的id号匹配的id属性的对象,您可以使用lo_dash库的_.find()函数。 https://lodash.com/docs#find

The call would look something like: _.find(data[0].articles, {id:req.params.id});

调用看起来像:_. find(data [0] .articles,{id:req.params.id});