我如何重构这个似乎重复很多相同事情的函数调用?
我已经实现了搜索博客文章的功能。但是,当添加到 containsQuery 数组时,我必须确保标题优先于摘录,摘录优先于内容。我编写了以下代码,它似乎运行良好,但似乎有很多冗余代码。我该如何改进这个?
const findArticles = (posts: any[], query: string) => {
const containsQuery: any[] = []
let words
let i: number
if (query.includes(' ')) {
words = query.toLowerCase().split(' ')
}
const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[] = [],) => {
if (multipleWords === false) {
for (i = 0; i < posts.length; i++) {
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
}
}
} else {
for (i = 0; i < posts.length; i++) {
if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
containsQuery.push(posts[i])
}
}
}
}
if (words) {
findArticle(true, posts, query, 'title', words,)
} else {
findArticle(false, posts, query, 'title')
}
if (words) {
findArticle(true, posts, query, 'excerpt', words,)
} else {
findArticle(false, posts, query, 'excerpt')
}
if (words) {
findArticle(true, posts, query, 'content', words,)
} else {
findArticle(false, posts, query, 'content')
}
const oneOfKind = Array.from(new Set(containsQuery.map(article => article.id))).map(id => {
return containsQuery.find(a => a.id === id)
})
return oneOfKind
}
为了避免重复并节省时间,我尝试将帖子复制到我自己的 const copyOfPosts = posts 中,然后按如下方式对其进行变异。但这最终由于某种原因破坏了代码。上面的代码似乎是我可以使其正常工作的唯一方法。任何建议都是非常受欢迎的。
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
copyOfPosts.splice(i, 1)
}
I have made function to search through blog posts. However, I have to make sure that titles take precedence over excerpt and excerpt over content when added to the containsQuery array. I have made the following code, it seems to be functioning well but seems there are a lot redundant code. How can I improve this?
const findArticles = (posts: any[], query: string) => {
const containsQuery: any[] = []
let words
let i: number
if (query.includes(' ')) {
words = query.toLowerCase().split(' ')
}
const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[] = [],) => {
if (multipleWords === false) {
for (i = 0; i < posts.length; i++) {
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
}
}
} else {
for (i = 0; i < posts.length; i++) {
if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
containsQuery.push(posts[i])
}
}
}
}
if (words) {
findArticle(true, posts, query, 'title', words,)
} else {
findArticle(false, posts, query, 'title')
}
if (words) {
findArticle(true, posts, query, 'excerpt', words,)
} else {
findArticle(false, posts, query, 'excerpt')
}
if (words) {
findArticle(true, posts, query, 'content', words,)
} else {
findArticle(false, posts, query, 'content')
}
const oneOfKind = Array.from(new Set(containsQuery.map(article => article.id))).map(id => {
return containsQuery.find(a => a.id === id)
})
return oneOfKind
}
In order to avoid duplicates and save time, I tried to copy the posts in to my own const copyOfPosts = posts and then mutate it as below. But this ended up breaking the code for some reason. Above code seems to be the only way I can make it work right. Any suggestion is most welcome.
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
containsQuery.push(posts[i])
copyOfPosts.splice(i, 1)
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这里:
我会以更好的方式重构
findArticle
这是因为我不喜欢在这种情况下使用过多的
else
,但人们可能会抱怨使用两个 <代码>返回。Here:
I would refactor
findArticle
in a better way for meThis is because I do not like use an excessive
else
like in this case, but people could complain about using tworeturn
.