I want to filter some posts from data.I try to use this code but it don't work.It run out post.frontmatter.title.filter is not a function
{posts.map((post, index) => (
post.frontmatter.title.filter(name=>name.includes('J')).map(filterdtitle=>(
<li>{filterdtitle}</li>
))
))}
I get the data of posts by getStaticProps like this and then I pass the posts to the function.The posts include title.I can get by {post.frontmatter.title},it is working.
const { data: frontmatter } = matter(markdownWithMeta)
return {
slug,
frontmatter
}
})
I want to know that how make it work.Thank you
CodePudding user response:
Looks like title is a string. filter() and includes() methods are available only for array.
CodePudding user response:
I believe you need to rewrite your code like this.
the title is a string, and the filter doesn't support string
you added an extra step by using a filter and include in the same line for a title.
{
posts.map((post, index) =>
post.frontmatter
.filter((name) => title.includes("J"))
.map((filterdtitle) => <li>{filterdtitle}</li>)
);
}
