Shopify Liquidで配列から任意の要素を取り除いた配列を生成する Tsun Inc.

Create an array in Shopify Liquid after removing specific elements

小笠原 京平

"RuffRuff 予約販売" supports the reduction of opportunity losses that occur when products are out of stock, new products are not on sale, or are on backorder.
It's incredibly easy to create pre-orders on your Shopify store, allowing you to register an unlimited number of pre-order products and accept unlimited pre-orders. You can also restrict people from ordering pre-order items and regular items at the same time, or send notifications when the item will be back in stock if it is not yet determined.
There are many unique features that are not found in other pre-order sales apps, such as the "Pay Later" feature that allows you to collect payment before the product is shipped.


My name is Ogasawara and I am CTO at Tsun Inc.!
Recently, I have been developing apps and themes for Shopify.
In this blog, we will share information useful for Shopify development.
I would be happy if this would be helpful for Shopify engineers!

Why did you decide to make it?

When working with an array in Shopify, I wanted to exclude part of it (like JavaScript's Array filter operation), but after researching, I found that Liquid does not have a process for excluding part of an array and creating a new array.

What you created

  • Processing to remove specific elements from an array in Liquid

Target audience

  • Basic knowledge about Liquid

Implementation

Prepare the target array

First, prepare the array before removing any elements. This time, as an example, we will create an array containing four strings: apple, orange, lemon, and banana. (Convert the string to an array with the split filter.)

{%- liquid
  # Array of ['apple', 'orange', 'lemon', 'banana']
  assign fruits = 'apple,orange,lemon,banana' | split: ','
-%}

Prepare an array of elements you want to remove

Similarly, create an array with the elements to be removed from the array created above. This time we will remove the orange and banana.

{%- liquid
  # Array of ['orange', 'banana']
  assign exclude_fruits = 'orange,banana' | split: ','
-%}

Remove any element from the target array

To remove specific elements from an array, first iterate through the array one by one using a for loop. Then, check whether each element is included in the elements to be removed (orange and lemon), and if it is not included, add it to a string. (Since you cannot add it directly to an array, add it to a string first.) Finally, by creating an array from the string using split, you can create an array that does not contain the specified elements from the target array.

{%- liquid
  # Array of ['apple', 'orange', 'lemon']
  assign fruits = 'apple,orange,lemon,banana' | split: ','
  # Array of ['orange', 'banana']
  assign exclude_fruits = 'orange,banana' | split: ','

  # Prepare the string that will form the array
  assign new_fruits_string = ''

  for fruit in fruits
    if exclude_fruits contains fruit
      continue
    endif
    if new_fruits_string == blank
      assign new_fruits_string = fruit
    else
      assign new_fruits_string = new_fruits_string | append: ',' | append: fruit
    endif
  endfor
  # Generate an array with arbitrary elements removed
  # Array of ['apple', 'lemon']
  assign new_fruits = new_fruits_string | split: ','
-%}

Summary

This time, we introduced how to remove specific elements from an array using Shopify Liquid!
It's a small topic, but I think it is a process that comes up fairly often when writing Liquid, so I would be happy if it was helpful.

Closing

Tsun Inc. uses Shopify to build stores and develop apps.
If you would like to build a store or develop an app, please contact Tsun Inc.

👆️目次はShopifyアプリ「RuffRuff 目次作成」を利用

👆️Table of contents by the Shopify app RuffRuff Table of Contents

Back to blog
  • Shopifyに棚卸し機能が登場!管理画面・POSでの操作手順を解説

    Shopify Introduces Stock Counts: How to Use The...

    Tsun ライターチーム

    Use Shopify's built-in stock counts to count inventory by location. Learn how to create a count and approve stock discrepancies in the Shopify admin, and how to count stock in...

    Shopify Introduces Stock Counts: How to Use The...

    Tsun ライターチーム

    Use Shopify's built-in stock counts to count inventory by location. Learn how to create a count and approve stock discrepancies in the Shopify admin, and how to count stock in...

  • Shopify Flowで商品の価格・タグ・メタフィールドの変更を検知する方法

    How to detect changes to product prices, tags, ...

    Tsun ライターチーム

    Use Shopify Flow’s Product updated trigger to detect changes to prices, tags, and metafields. See how to tag products for review when prices change and how to test the workflow.

    How to detect changes to product prices, tags, ...

    Tsun ライターチーム

    Use Shopify Flow’s Product updated trigger to detect changes to prices, tags, and metafields. See how to tag products for review when prices change and how to test the workflow.

  • Shopifyのファイル管理を徹底解説|対応形式・容量上限・フォルダ整理の方法

    Shopify File Management: Formats, Storage Limit...

    Tsun ライターチーム

    Learn how to manage images, videos, and PDFs in the Shopify admin. Check supported file formats and plan limits, then learn to create folders, move files, search, and delete.

    Shopify File Management: Formats, Storage Limit...

    Tsun ライターチーム

    Learn how to manage images, videos, and PDFs in the Shopify admin. Check supported file formats and plan limits, then learn to create folders, move files, search, and delete.

  • Shopifyと連携できる外部AI7選!対応機能・接続方法を比較

    7 AI Tools That Connect to Shopify: Features an...

    Tsun ライターチーム

    Compare seven AI tools that connect to Shopify. See which ones fit tasks like adding product images, analyzing sales, and working with Microsoft 365, and check the permissions and limits...

    7 AI Tools That Connect to Shopify: Features an...

    Tsun ライターチーム

    Compare seven AI tools that connect to Shopify. See which ones fit tasks like adding product images, analyzing sales, and working with Microsoft 365, and check the permissions and limits...

  • Shopifyのストア分析・レポートを画像付きで解説|使い方からカスタマイズまで

    A visual guide to Shopify Analytics and reports...

    Tsun ライターチーム

    Learn how to break down sales and sessions in Shopify Analytics, compare data across the session measurement update, and use bubble maps, custom data, and annotations.

    A visual guide to Shopify Analytics and reports...

    Tsun ライターチーム

    Learn how to break down sales and sessions in Shopify Analytics, compare data across the session measurement update, and use bubble maps, custom data, and annotations.

  • Shopifyで1つのバリエーションに複数のバーコードを登録する方法

    Shopifyで1つのバリエーションに複数のバーコードを登録する方法

    Tsun ライターチーム

    Shopifyでは、1つの商品やバリエーションに最大20個のバーコードを登録できます。管理画面とCSVでの登録手順、SKUとの違い、Shopify POSでの活用方法、外部サービス連携時の注意点を解説します。

    Shopifyで1つのバリエーションに複数のバーコードを登録する方法

    Tsun ライターチーム

    Shopifyでは、1つの商品やバリエーションに最大20個のバーコードを登録できます。管理画面とCSVでの登録手順、SKUとの違い、Shopify POSでの活用方法、外部サービス連携時の注意点を解説します。

1 of 6
  • AI仮想顧客の改善ツール「SimGym」を使ったら売上はどうなった?実行から3か月後までの記録

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

    What Happened to Sales After Using SimGym, an A...

    テキサス・アユミ

    A record of trying SimGym on the miso specialty store ドットミソ, from checking the proposals through carrying out changes to the look-back three months later. It covers how the order...

  • 【ストアインタビュー後編】やりながら育てるShopify。「野菜をMOTTO」の試行錯誤:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

    [Store Interview, Part 2] Building and Growing ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." An in-depth look at our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its...

  • 【ストアインタビュー前編】 ECが主戦場でも、ECが目的ではない。「野菜をMOTTO」が貫くEC戦略:モンマルシェ株式会社 河野雄士さん

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

    [Store Interview, Part 1] Even When E-Commerce ...

    テキサス・アユミ

    "Even when e-commerce is the main battlefield, e-commerce isn't the goal." Part 1 of our interview with Yuji Kono of Mon Marché, whose brand "Yasai wo MOTTO" grew its e-commerce...

1 of 3
  • Our Editorial Process

    For readers new to e-commerce and Shopify, we write our articles with care to explain difficult commerce and marketing terms, along with how everything works from building a store through to selling, as “gently,” “clearly,” and “accurately” as we can.

    Learn more 
  • About the information on this site

    The information published here is based on Tsun Inc.’s own experience and research. While we make every effort to publish information that is as “current” and “accurate” as possible, we cannot fully guarantee its contents. Tsun Inc. accepts no liability whatsoever for any damages arising from this site. If you find an error in the information published here, we would be grateful if you would contact Tsun Inc.