Forum Discussion

DavidO's avatar
DavidO
Strategist II
2 years ago

Has anyone got 'map' and 'join' to work on a liquid array? Code review please :)

Hi all I have been looking at options to capitalize the first letter of both names for users who have two first names saved in our database. For example: 'tony peter' becomes 'Tony Peter'. I ca...
  • Arso's avatar
    2 years ago

    DavidO map only works on properties and the property name should be where you have 'capitalize' in teh code. 'capitalize' is not a property of any item in your array, that's why this won't work. 

    Try this:

    {% assign fullName = "tony peter" %}
    {% assign names = fullName | split: ' '  %}
    {%- capture capitalizedNames -%}
      {%- for name in names %}{{ name | capitalize }} {% endfor -%}
    {%- endcapture -%}
    {{ capitalizedNames }}