使用过滤数组中的 JSON 对象快速回复视图
我想返回一个视图,其中包含已根据 GET 请求中的 URL 从 JSON 数组中筛选出的数据。
使用 Fastify,我有一个 GET 路由来处理博客下的页面,例如 example.com/blog/blogPageTitle。
我使用传入的 url 标题来过滤 json 文件 (posts.json) 中的数组帖子,并使用观点 fastify 插件返回视图:
const posts = require('./../posts.json')
fastify.get('/blog/:urlTitle', async (request, reply) => {
// e.g. request.params.urlTitle = "blogPageTitle"
let data = posts.filter(items => items.urlName === request.params.urlTitle)
reply.view('/templates/workItem.ejs', data)
})
posts.json 我正在过滤的 JSON 文件:
[
{
urlName: 'blogPageTitle-1',
title: 'Post title 1',
subTitle: 'blah blah blah',
tags: [
'history',
'geography',
'another theme'
]
},
{
urlName: 'blogPageTitle-2',
title: 'Post title 2',
subTitle: 'blah blah blah',
tags: [
'history'
]
},
{
urlName: 'blogPageTitle-3',
title: 'Post title 3',
subTitle: 'blah blah blah',
tags: [
'maths'
]
}
]
变量数据返回带有单个对象的数组:
[
{
title: 'Post title',
subTitle: 'blah blah blah',
tags: [
'history',
'geography',
'another theme'
]
}
]
我只想返回对象,而不是数组 >目的。
在我的 EJS 模板页面中,我有以下内容:
<h1><%= title %></h1>
<h2><%= subTitle %></h2>
<ul>
<% tags.forEach(tag => { %>
<li><%= tag %></li>
<% }) %>
</ul>
我从 EJS 收到 500 错误,指出 <%= title %>没有定义。如果我把它写成 <%= data.title %>那么这似乎可行,但是我不想访问页面上的数组对象。
我有两个问题:
- 这是搜索 json 以获得单个相关帖子的最佳方式吗?
- 如何仅返回带有发布数据的对象,而不是数组?
I want to return a view with data that has been filtered from a JSON array based on the URL in the GET request.
Using Fastify I have a GET route that handles the pages under blog for example example.com/blog/blogPageTitle.
And I use the incoming url title to filter an array posts that are in a json file (posts.json), and return the view using the point-of-view fastify plugin:
const posts = require('./../posts.json')
fastify.get('/blog/:urlTitle', async (request, reply) => {
// e.g. request.params.urlTitle = "blogPageTitle"
let data = posts.filter(items => items.urlName === request.params.urlTitle)
reply.view('/templates/workItem.ejs', data)
})
posts.json JSON file I am filtering:
[
{
urlName: 'blogPageTitle-1',
title: 'Post title 1',
subTitle: 'blah blah blah',
tags: [
'history',
'geography',
'another theme'
]
},
{
urlName: 'blogPageTitle-2',
title: 'Post title 2',
subTitle: 'blah blah blah',
tags: [
'history'
]
},
{
urlName: 'blogPageTitle-3',
title: 'Post title 3',
subTitle: 'blah blah blah',
tags: [
'maths'
]
}
]
The variable data is returning an array with a single object:
[
{
title: 'Post title',
subTitle: 'blah blah blah',
tags: [
'history',
'geography',
'another theme'
]
}
]
I want to return just the object, rather than the array > object.
In my EJS template page I have the following:
<h1><%= title %></h1>
<h2><%= subTitle %></h2>
<ul>
<% tags.forEach(tag => { %>
<li><%= tag %></li>
<% }) %>
</ul>
I get a 500 error from EJS stating that the <%= title %> is not defined. If I put this as <%= data.title %> then this seems to work, however I don't want to have to access the arrayed object on a page.
I have two questions:
- Is this the best way to search through the json to get the single related post?
- How do I return just an object with the post data, rather than an array?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在我看来,您想使用
Array.prototype.find
方法。这会过滤列表并查找第一个匹配的对象,如果没有匹配则查找undefined
。在您的示例中:
应返回标题包含
request.Params
中的urlTitle
的第一个post
。It sounds to me like you want to be using the
Array.prototype.find
method. This filters a list and finds the first matching object orundefined
if there are no matches.In your example:
Should return the first
post
whose title contains theurlTitle
from therequest.Params
.