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.
Here's a basic example to get unique values from an array:
{% set array = [1, 2, 2, 3, 4, 4, 5] %} {% set uniqueValues = [] %} {% for item in array %} {% if item not in uniqueValues %} {% set _ = uniqueValues.append(item) %} {% endif %} {% endfor %} {{ uniqueValues }}
This script will output [1, 2, 3, 4, 5]
, which contains only the unique values from the original array.
Now, let's integrate this concept into Jira and JMWE. Suppose you want to extract unique linked project keys from issues in a certain initiative. Here's how you can achieve that using JMWE in Jira:
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 theuniqueProjects
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.