使用过滤数组中的 JSON 对象快速回复视图

发布于 2025-01-19 07:18:05 字数 1682 浏览 2 评论 0原文

我想返回一个视图,其中包含已根据 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 %>那么这似乎可行,但是我不想访问页面上的数组对象。

我有两个问题:

  1. 这是搜索 json 以获得单个相关帖子的最佳方式吗?
  2. 如何仅返回带有发布数据的对象,而不是数组?

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:

  1. Is this the best way to search through the json to get the single related post?
  2. How do I return just an object with the post data, rather than an array?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

习惯成性 2025-01-26 07:18:06

在我看来,您想使用 Array.prototype.find 方法。这会过滤列表并查找第一个匹配的对象,如果没有匹配则查找 undefined

在您的示例中:

const posts = require('./../posts.json')
fastify.get('/blog/:urlTitle', async (request, reply) => {
    // e.g. request.params.urlTitle = "blogPageTitle"
    const data = posts.find({ urlName } => urlName.includes(request.params.urlTitle))       
     
    reply.view('/templates/workItem.ejs', data) 
})

应返回标题包含 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 or undefined if there are no matches.

In your example:

const posts = require('./../posts.json')
fastify.get('/blog/:urlTitle', async (request, reply) => {
    // e.g. request.params.urlTitle = "blogPageTitle"
    const data = posts.find({ urlName } => urlName.includes(request.params.urlTitle))       
     
    reply.view('/templates/workItem.ejs', data) 
})

Should return the first post whose title contains the urlTitle from the request.Params.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文