【问题标题】:how to filter and get limit amount of data using javascript如何使用javascript过滤和获取限制数量的数据
【发布时间】:2021-09-07 22:11:03
【问题描述】:

我有一个包含一些 java 脚本对象的数组,我正在使用 ejs 来显示数据,但问题是 - 我必须在 req.params.id 的基础上过滤该数据并想要下一个 2 个数据对象..!过滤数据后——请帮忙

我的代码是 -

app.get("/:id", (req, res) => {
  const requestParams = _.lowerCase(req.params.id);

  let obj = blogData.find((o) => o.heading >= "Yoga-During-COVID");
  console.log(obj);
 
  blogData.forEach(function (post) {
    const storedTitel = _.lowerCase(post.heading);
    
    if (storedTitel === requestParams) {
      res.render("blogfullDetail", {
        date: post.date,
        heading: post.heading,
        subheading: post.subheading,
        discription: post.discription,
        discription2: post.discription2,
        author: post.author,
        authorImage: post.authorImage,
        mainImg: post.mainImg,
      });
    }
  });

}); 

数据文件-

【问题讨论】:

  • do not post pictures of text,将该文本放入您的帖子中(使用适当的标记)。话虽如此,我不确定我是否理解您的需求,您能否(在您的帖子中)解释您拥有哪些数据,以及您希望代码做什么,以便获得您需要的数据(您在其中展示示例您拥有的数据,以及您需要成为的数据)。
  • 我有一个对象数组,实际上我想过滤图片中的数据,过滤后需要的对象在 req.params.id 值 @Mike'Pomax'Kamermans 之后
  • 请您解释一下in your post。再次,请用真实文本替换该图像。

标签: javascript node.js ejs


【解决方案1】:

使用findIndexslice 的组合。 findIndex 返回匹配元素的索引, slice 返回从该索引到计算出的更高索引的子数组...

let blogData = [{
    id: 1,
    title: 'yoga is good'
  },
  {
    id: 32,
    title: 'yoga helps you stretch'
  },
  {
    id: 12,
    title: 'covid yoga is good too'
  },
  {
    id: 41,
    title: 'no such thing as too much yoga'
  },
  {
    id: 57,
    title: 'is hot yoga too hot'
  }
];

// say you need the post about covid...
let targetTitle = 'covid yoga is good too';
let index = blogData.findIndex(p => p.title === targetTitle);

// with the index, answer a slice of the blogs
// starting at that index, ending 3 later
let posts = blogData.slice(index, index + 3);
console.log(posts)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-02-28
    • 2019-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-11-04
    • 1970-01-01
    • 2018-08-14
    相关资源
    最近更新 更多