如何使用 javascript 过滤和获取限制量的数据
how to filter and get limit amount of data using javascript
我有一个数组,其中包含一些 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,
});
}
});
});
数据文件-
使用 findIndex
和 slice
的组合。 findIndex returns 匹配元素的索引和 slice returns 从该索引到计算出的更高索引的子数组...
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)
我有一个数组,其中包含一些 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,
});
}
});
});
数据文件-
使用 findIndex
和 slice
的组合。 findIndex returns 匹配元素的索引和 slice returns 从该索引到计算出的更高索引的子数组...
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)