Skip to end of banner
Go to start of banner

Retrieve unique values from an array in Nunjucks

Skip to end of metadata
Go to start of metadata

You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

Abstract: When working with arrays in specific contexts, such as extracting unique values, it's crucial to employ appropriate techniques to avoid duplicates. This article guides you through resolving such issues when attempting to obtain unique values from an array, particularly in the context of linked issues.

Problem Description: In a scenario, where you're trying to extract unique values from an array, encountering duplicate values can be a common challenge.

For example, retrieving unique project names from linked issues might result in duplicates due to multiple associations or other factors.

Solution: To address this issue effectively, you can utilize the following approach, ensuring that only distinct values are retained:

{% set linkIssues = issue | linkedIssues("Initiative Contains") | field("fields.project.key") %}
{% set uniqueProjects = [] %}

{% for project in linkIssues %}
    {% if uniqueProjects.indexOf(project) === -1 %}
        {% set _ = uniqueProjects.push(project) %}
    {% endif %}
{% endfor %}

{{ uniqueProjects }}

Explanation:

  • The linkedIssues function fetches the linked issues based on the specified link type, in this case, "Initiative Contains."

  • We extract the project keys from the linked issues using the field function.

  • An empty list uniqueProjects is initialized to store unique project keys.

  • In the loop, each project key is checked against the uniqueProjects list.

  • If the project key is not already present (indexOf(project) === -1), it's appended to the uniqueProjects list.

  • Finally, the unique project keys are printed out.

Conclusion: Following the provided approach, you can effectively extract unique values from an array while mitigating duplicate entries. This ensures accurate data representation, particularly in project management contexts where distinct project associations are essential.

  • No labels