1

I'm new to TWIG...
Within a TWIG template, I need to create a new array of objects that is a subset of an existing array of objects (products).

So far, I have this:

{% set onlyLimitedAmtProds = [] %}
{% for product in products %}
    {% if product.in_stock_amount < 3 %}
        {% set onlyLimitedAmtProds = onlyLimitedAmtProds|merge(product) %}
    {% endif %} 
{% endfor %}

The above generates an error of:
"The merge filter only works with arrays or hashes; array and object given."

I'm not sure of the syntax to make this work.

1 Answer 1

1

You are actually trying to merge array onlyLimitedAmtProds with object product. But merge function works only with two arrays. You need to merge with an array of one element: merge([product]).

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.