Replace integer field value with stars in twig template
For the field template there are two approaches to get the integer number from a field:

Node object

{% set difficulty = element['#object'].field_difficulty.value %}
{% for i in range(1, 5) %}
  {% if i &lt;= difficulty %}


Find the rendered number

You can try to find the number in #markup:

{% if loop.index &lt;= item.content['#markup'] %}


The node object is the better approach, because the way how the number is rendered can change when updating the code or changing the configuration.