Function to filter comments and ratings, with pagination support I have a web component that is rendering a list of items and I have this method for filtering these items based in a filterBy object that is being updated by other events and stored in the same component class.

filterBy = {
    comment: '', // string
    rating: [] // array of numbers
}

filterFeedback(items) {
    let filteredItems = [];
    const pagination = { offset: 0, limit: 10, totalCount: items.length };
    const { comment, rating } = this.filterBy;

    const filteringFn = (condition) =&gt; {
        items.map((item) =&gt; {
            if (filteredItems.length &lt; pagination.limit) {
                if (condition(item)) {
                    filteredItems.push(item);
                }
            } else {
                return;
            }
        });
    }
    if (comment &amp;&amp; (!rating || rating.length === 0) &amp;&amp; comment !== '') {
        filteringFn((item) =&gt; item.comment &amp;&amp; item.comment.includes(comment));
    } else if (!comment &amp;&amp; rating &amp;&amp; rating.length &gt; 0) {
        filteringFn((item) =&gt; rating.some((value) =&gt; value === item.rating));
    } else if ((comment &amp;&amp; comment !== '' )&amp;&amp; rating &amp;&amp; rating.length &gt; 0) {
        filteringFn((item) =&gt; item.comment &amp;&amp; item.comment.includes(comment) &amp;&amp; rating.some((value) =&gt; value === item.rating));
    } else {
        filteredItems = items ? items.filter((item, i) =&gt; i &lt; pagination.limit) : [];
    }

    return filteredItems;
}


I was analyzing this code and trying to make it more clear and readable. I think the filteringFn saves me a lot of code duplication but still, I found the conditionals unreadable. Besides, it's not scalable in case I'd like to add more filters.
If one of your filters is undefined, just set it to always succeed.

If one of your parameters is undefined, set it to something so that you don't have to check it over and over.

Truncate output to pagesize at the end.

filterFeedback(items) {
    const pagination = { offset: 0, limit: 10, totalCount: items.length };
    const filtRatings=new Set( 
        ( this.filterBy.rating &amp;&amp; this.filterBy.rating.length ) ?
          this.filterBy.rating : items.map( item =&gt; item.rating ) 
    );
    const filtComment=this.filterBy.comment || '';

    return items.filter( item =&gt; 
        ( item.comment || '' ).includes(filtComment) 
        &amp;&amp; 
        filtRatings.has(item.rating) 
    )
    .slice(0,pagination.limit);

}