He creado una función para buscar en las publicaciones del blog. Sin embargo, debo asegurarme de que los títulos tengan prioridad sobre el extracto y el extracto sobre el contenido cuando se agreguen a la matriz containsQuery. He creado el siguiente código, parece funcionar bien, pero parece que hay mucho código redundante. ¿Cómo puedo mejorar esto?
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 }Para evitar duplicados y ahorrar tiempo, traté de copiar las publicaciones en mi propio const copyOfPosts = posts y luego muté como se muestra a continuación. Pero esto terminó rompiendo el código por alguna razón. El código anterior parece ser la única forma en que puedo hacer que funcione correctamente. Cualquier sugerencia es bienvenida.
if (posts[i][`${searchedParam}`].toLowerCase().includes(query.toLowerCase())) { containsQuery.push(posts[i]) copyOfPosts.splice(i, 1) }Aquí:
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 } Refactorizaría findArticle de una mejor manera para mí
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 } Esto se debe a que no me gusta usar un else excesivo como en este caso, pero la gente podría quejarse de usar dos return .