搜索节点中的对象数组

时间:2017-03-23 19:16:44

标签: javascript arrays node.js object express

我有一个对象数组,每个对象包含以下数据:enter image description here

我有一个节点路由,它引入一个标签然后搜索它。现在我输入的任何东西都返回-1。如果标记与查询匹配,我如何获得修改后的数组。

此时,我输入的任何内容(包括有效标记)都会返回-1。

此时,我会转而使用修改后的数组并将其注入模板中,如您所见。

router.get('/search/:tag', function(req, res) {
  const tag = req.params.tag;
  shopify.article.list(86289414)
  .then(function (response) {
    response.reverse();

    index = response.indexOf(response.filter(function(item) {
      return item.tags == tag
  }) )

  console.log(index);

    var data = {
      articles: index.map((article) => {
        return {
          author: article.author,
          id: article.id,
          html: article.body_html,
          tags: article.tags,
          date: moment(article.published_at).format("Do MMM YYYY"),
          slug: article.handle,
        } // return
      }) // map
    } // data

      res.json(data);

  }) // then
    .catch(err => console.log(err) )
});

这样做有什么好办法?

谢谢!

P.S。我查看了lodash并找不到任何东西。我对lodash持开放态度。

1 个答案:

答案 0 :(得分:0)

我用以下代码解决了它。

response = response.filter(function(item) {
     return item.tags.indexOf(tag) > -1;
   });
相关问题