Home > Shopify Development > How to Get Item with specified index in Array in Liquid

How to Get Item with specified index in Array in Liquid in Shopify

Sam Nguyen
Sam Updated: April 15, 2024

Share:

Drive 20-40% of your revenue with Avada
avada email marketing

Your online is encountering the inconvenience in showing specific items or product tags which is neither the first one nor the last one in your product page. If you are using Liquid template language, you can take a look to view these steps below to help you gather items. The tutorial aims to assist you in getting the item with specified index in array in liquid.

Table of content

What is Liquid template language?

The liquid is one of the open-source template languages in Shopify. Liquid code is clasified into three different types consisting of objects, tags and filters. The tutorial today focuses on the array filter, one of helpful function of this website language. The liquid is used as the backbone of Shopify to upload dynamic content on the front page.

Information about the array filter and index liquid?

In order to create the product array, you star with the capture tag so that you can set a new product list. When assigning value to each element, you separate each value with a singe product using a | and each set of product value with ::. The :: itself is qualified with a conditional statement. Once the product list is complete, you also use :: to create a new varialble

Array filter is a function used to create a new array from a provided array including names or items which meet the demand of the condition set by the argument function. In general, the formation of an array looks like this:

sample array filter

The filter feature is used to offer callback function for each character in an array and build a new array that can also give the value back to confirm the true feature of that element. The callback is compatible with three arguments: the value of the element, the index of the element and the array objects being traversed.

In addition, the range of elements demonstrated by the filter is set by the first petition of the callback. If existing elements are changed or eliminated, their value will be passed to the filter hence when filter check these elements, their value is retained.

Index.liquid is belonged to the array filter. The index.liquid refers to the homepage of the store. This template is loaded when you visit the root of the store. This template helps to return an item at a specific location in an array.

How to get the item with specified index in array in liquid

Here is one simple formula that you can follow. Attention: array numbering begins from zero, the first item in an array will be attached with [Ø]. For instance, if you want to show “women” in an array, you do these steps:

Input:


<!-- product.tags = "sale", "mens", "womens", "awesome" -->
{{ product.tags[2] }}

Output:

womens

Conclusion

In conclusion, getting item wwith specified index in arrat in liquid may make the shopping journey become easier. Buyers will find it more convenient when shopping around because they do not need much time to seek key tags or information.


Sam Nguyen is the CEO and founder of Avada Commerce, an e-commerce solution provider headquartered in Singapore. He is an expert on the Shopify e-commerce platform for online stores and retail point-of-sale systems. Sam loves talking about e-commerce and he aims to help over a million online businesses grow and thrive.

People also searched for

Stay in the know

Get special offers on the latest news from AVADA.