Forum Discussion
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 can make it work with a for loop but the docs suggest 'map' and 'join' should work which would considerably reduce the liquid.
Here is what I have that just won't play ball and I can't seem to find where it is failing:
{% assign fullName = "tony peter" %}
{% assign names = fullName | split: ' ' %}
{% assign capitalizedNames = names | map: 'capitalize' %}
{{ capitalizedNames | join: ' ' }}
I'm looking to split the first names at the space into an array so they can be worked on one at a time. The liquid then maps over the array, capitalizing each first letter. Once complete, rejoining into a complete string with a space in the middle.
Any advice would be great.
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 }}
- ArsoPractitioner III
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 }}
Related Content
- 7 months ago