Skip to content

Filter to exclude item from string array #890

Description

@dave-kennedy

I want to iterate over an array but exclude a certain value. The reject filter seems to be the tool for arrays of objects, but what about arrays of strings?

The ever-helpful-and-wise search assist AI built into DuckDuckGo had this to say:

To exclude specific elements from a string array in LiquidJS, you can utilize the reject filter. This filter allows you to remove items based on a defined condition.

Using the reject Filter

The reject filter works by evaluating each item in the array against a condition. If the condition is met, the item is excluded from the resulting array.

Example Syntax

Here’s a basic example of how to use the reject filter:

{% assign myArray = "apple, banana, cherry, date" | split: ", " %}
{% assign filteredArray = myArray | reject: "banana" %}

This must be a hallucination because it contradicts the documentation, but it also seems intuitive and worth supporting.

I know I can make a custom filter (I would call it exclude or except) but just wanted to verify there isn't something already built-in. If not, would the maintainers accept a PR to add it?


Screenshot for proof:

Image

Metadata

Metadata

Assignees

No one assigned

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions