我该如何重构这个似乎重复了很多相同事情的函数调用?

How can I refactor this function call that seems to repeat alot of the same thing over over again?

我已经制作了搜索博客文章的功能。但是,当添加到 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)
          }

这里:

const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[]) => {
    const containsQuery: any[] = []
    if (multipleWords === false) {
        for (let i = 0; i < posts.length; i++) {
            if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
                containsQuery.push(posts[i])
            }
        }
    } else {
        for (let i = 0; i < posts.length; i++) {
            if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
                containsQuery.push(posts[i])
            }
        }
    }
    return containsQuery
}

const findArticles = (posts: any[], query: string) => {

    let words = query.includes(' ') ? query.toLocaleLowerCase().split(' ') : [] 

    const containsQuery = findArticle(true, posts, query, 'title', words)

    containsQuery.push(...findArticle(true, posts, query, 'excerpt', words))

    containsQuery.push(...findArticle(true, posts, query, 'content', words))

    const oneOfKind = Array.from(new Set(containsQuery.map(article => article.id))).map(id => {
        return containsQuery.find(a => a.id === id)
    })
    
    return oneOfKind
}

我会以对我来说更好的方式重构 findArticle

const findArticle = (multipleWords:boolean, posts: any[], query:string, searchedParam:string, words:string[]) => {
    const containsQuery: any[] = []
    if (multipleWords === false) {
        for (let i = 0; i < posts.length; i++) {
            if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) {
                containsQuery.push(posts[i])
            }
        }

        return containsQuery
    } 
    
    for (let i = 0; i < posts.length; i++) {
        if(words.every(q => posts[i][`${searchedParam}`].toLowerCase().includes(q))) {
            containsQuery.push(posts[i])
        }
    }
    return containsQuery
}

这是因为我不喜欢在这种情况下使用过多的 else,但人们可能会抱怨使用两个 return