FROM CACHE - en_header

iterate over properties in an object

jmerrick
Tourist
5 0 1

I'm trying to write a loop that will list the properties of an object. Not necessarily the values of the properties, just a list of the properties a particular object has.

Does anyone know how to do that?

Reply 1 (1)
malalta
Shopify Partner
23 5 14

It'd be great if there was a consistent way to iterate over an object, but as it stands the most complete information is in the Shopify liquid docs if you're looking at built in objects.

The next best option is to use the "to json" filter. For example:

<!-- Assuming you're in a product liquid template... -->
<script>console.log( {{ product | json }} );</script>

This will give you most object keys (and values). However:

  • The json filter will exclude a few things (from memory the inventory details on variants are one example). However these properties are still available on the liquid object within your templates, they just get excluded in the json filter's output. You'll need to refer to the docs for the full picture.
  • The json filter is available on most objects, but not all. For example, it won't work on {{ section }}.

Hope that helps!

Andrew.