【发布时间】:2022-01-26 18:06:51
【问题描述】:
我正在使用 react+ nextJS 并抓取静态对象“帖子”。目标是在每个帖子上创建一个“相关帖子”组件,该组件抓取三个包含至少一个类别的帖子。这是我在 AllPosts 上运行地图时的样子(我把它剪掉了,这样更容易阅读):
const allPosts = getAllPosts(['title', 'categories'])
[{
title: 'Black History Month: A History in Black Cinematography',
categories: [ 'education' ]
}
{
title: 'New Kid on the Block',
categories: [ 'announcements', 'new hires' ]
}
{
title: 'Olivia Boldt: Bol(d)ting Into the Bakery',
categories: [ 'announcements', 'new hires' ]
}
{
title: 'PDF Presets in Adobe Illustrator. Which one should you use?',
categories: [ 'Research', 'education' ]
}
{
title: 'Pixel Bakery does XYZ',
categories: [ 'press & media', 'announcements' ]
}
{
title: 'Recipe for Success: Mix Adaptability and Confidence (together, in a medium sized bowl)',
categories: [ 'editorial', 'second cat', 'third cat' ]
}
{
title: 'Samee Callahan: A Winding Path to Excitement',
categories: [ 'announcements', 'new hire' ]
}
{
title: 'Sophia Stueven’s Favorite Way to Breathe',
categories: [ 'announcements', 'new hires' ]
}]
到目前为止,我可以让它匹配具有相同 [0] 类别的帖子,但我无法思考如何比较所有帖子:
const allPosts = getAllPosts(['title', 'categories'])
const SearchCat = post.categories[0]
const matchingPosts = allPosts.filter((item) => item.categories[0] === SearchCat)
console.log(matchingPosts)
产量:
[{
title: 'An Introduction to our Technology Stack',
categories: [ 'announcements', 'new hire' ]
},
{
title: 'New Kid on the Block',
categories: [ 'announcements', 'new hires' ]
},
{
title: 'Olivia Boldt: Bol(d)ting Into the Bakery',
categories: [ 'announcements', 'new hires' ]
},
{
title: 'Samee Callahan: A Winding Path to Excitement',
categories: [ 'announcements', 'new hire' ]
},
{
title: 'Sophia Stueven’s Favorite Way to Breathe',
categories: [ 'announcements', 'new hires' ]
}]
如何添加另一个过滤器层,它不关心类别匹配所在的位置,只需要其中一个匹配?
【问题讨论】:
-
@pilchard 是的!还有几个问题 1)如何让搜索忽略大小写敏感 2)如何从返回的列表中排除当前帖子和 3)Typescript 不喜欢 .some:“类型上不存在属性 'some' '字符串'”,我该如何解决?
-
@pilchard 另外,很抱歉重复的问题。我什至不知道该谷歌什么。
标签: javascript arrays reactjs next.js