Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
Locked
0

How to create an array?

Explorer ,
Oct 05, 2016 Oct 05, 2016

Hi all,

I am trying to lay out a Web App with approx 300 fields containing product specifications.

Each of these fields also needs a label associated with it - something like this:

<ul>

<li>Input Voltage: {{voltage}}</li>

<li>Retransmission Current: {{current}}</li>

<li>Excitation Voltage: {{excitation}}</li>

... onwards to 300 fields!

</ul>

Obviously the above will work, but I was wondering whether there is a way to take my 300 fields and add them to a JSON array so that I can loop through them - something like the below?

[

     { "label": "Input Voltage", "value": "{{voltage}}" },

     { "label": "Retransmission Current", "value": "{{current}}"},

     { "label": "Excitation Voltage", "value": "{{excitation}}"}

]

I would be so grateful if someone could point me to an example of how to do this, if it's possible. I have hunted through BC knowledgebase and forums for several hours and found a lot of info on how to manipulate an existing array, but not how to create one...

Really appreciate any help you can give, and apologies if this has been answered elsewhere - I really did try to help myself before posting!

TOPICS
Developer
566
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Enthusiast , Oct 05, 2016 Oct 05, 2016

Creating arrays of objects isn't practical in Liquid. Instead, I'd create two arrays of Strings, maybe something like this:

    {% assign keys = "voltage|excitation|current" | split: "|" -%}
    {% assign labels = "Input Voltage|Retransmission Current|Excitation Voltage" | split: "|" -%}
   
    <ul>
        {% for key in keys -%}
            <li>{{ labels[forloop.index0] }}: {{ webappitems[key] }}</li>
        {% endfor -%}
    </ul>
   

The webappitems part will have to change, depending on context.

Translate
Enthusiast ,
Oct 05, 2016 Oct 05, 2016

Creating arrays of objects isn't practical in Liquid. Instead, I'd create two arrays of Strings, maybe something like this:

    {% assign keys = "voltage|excitation|current" | split: "|" -%}
    {% assign labels = "Input Voltage|Retransmission Current|Excitation Voltage" | split: "|" -%}
   
    <ul>
        {% for key in keys -%}
            <li>{{ labels[forloop.index0] }}: {{ webappitems[key] }}</li>
        {% endfor -%}
    </ul>
   

The webappitems part will have to change, depending on context.

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Explorer ,
Oct 05, 2016 Oct 05, 2016
LATEST

That is exactly the kind of solution I was hoping for and works perfectly with my Web App - thank you SO much, Robert!

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines