Comment parcourir une liste de tableaux associatifs regroupés par des valeurs clés spécifiques ?Disons que j’ai une liste de tableaux associatifs.
Je souhaite parcourir cette liste par groupes. D'abord par catégorie, puis, dans une boucle imbriquée, par sous-catégorie.
aalist = {
[
"Category" => "A",
"Subcategory" => "aa",
"Value" => "123"
]
,
[
"Category" => "A",
"Subcategory" => "aa",
"Value" => "124"
]
,
[
"Category" => "A",
"Subcategory" => "bb",
"Value" => "125"
]
,
[
"Category" => "A",
"Subcategory" => "bb",
"Value" => "124"
]
,
[
"Category" => "A",
"Subcategory" => "cc",
"Value" => "125"
]
,
[
"Category" => "B",
"Subcategory" => "aa",
"Value" => "126"
]
,
[
"Category" => "B",
"Subcategory" => "aa",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "bb",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "bb",
"Value" => "126"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "126"
]
};Cela signifie que dans ma première boucle, lors de la première itération, j'obtiens une liste de tableaux associatifs dont la catégorie est « A ». Dans la boucle imbriquée, j'effectue une itération à travers les sous-catégories, où, lors de la première itération, j'obtiens une liste de tous les tableaux associatifs dont la sous-catégorie est « aa », et ainsi de suite.
Un bon exemple de ce que je dois accomplir peut être montré dans PowerShell comme suit :
# Sample data
$data = @(
[PSCustomObject]@{ Name = "Alice"; Group = "A" }
[PSCustomObject]@{ Name = "Bob"; Group = "B" }
[PSCustomObject]@{ Name = "Charlie"; Group = "A" }
[PSCustomObject]@{ Name = "David"; Group = "B" }
)
# Group the data by the 'Group' property
$groupedData = $data | Group-Object -Property Group
# Iterate over each group
foreach ($group in $groupedData) {
Write-Host "Group: $($group.Name)"
# Iterate over each item within the group
foreach ($item in $group.Group) {
Write-Host " Name: $($item.Name)"
}
}Comment puis-je faire quelque chose comme ça en JSL ?
Automatisation et scriptsRe : Comment parcourir une liste de tableaux associatifs regroupés par des valeurs clés spécifiques ?Ok, voici comment je l'ai fait en JSL :
aalist = {
[
"Category" => "A",
"Subcategory" => "aa",
"Value" => "123"
]
,
[
"Category" => "A",
"Subcategory" => "aa",
"Value" => "124"
]
,
[
"Category" => "A",
"Subcategory" => "bb",
"Value" => "125"
]
,
[
"Category" => "A",
"Subcategory" => "bb",
"Value" => "124"
]
,
[
"Category" => "A",
"Subcategory" => "cc",
"Value" => "125"
]
,
[
"Category" => "B",
"Subcategory" => "aa",
"Value" => "126"
]
,
[
"Category" => "B",
"Subcategory" => "aa",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "bb",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "bb",
"Value" => "126"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "127"
]
,
[
"Category" => "B",
"Subcategory" => "cc",
"Value" => "126"
]
};
// Function to group by a property
groupBy = Function( {list, property},
{Default local},
groups = Associative Array();
For Each({item, index}, list,
key = item[property];
If( !Contains( groups, key ),
groups[key] = {};
);
Insert Into( groups[key], item );
);
groups;
);
// Group the list by the "Department" property
grouped = groupBy(aalist, "Category");
// Iterate over the groups
For Each( {{key, value}, index}, grouped,
Write("\!nCategory: " || key);
subgrouped = groupBy(value, "Subcategory");
For Each( {{key, value}, index}, subgrouped,
Write("\!n - Subcategory: "|| key);
For Each({item, index}, value,
Write("\!n - Category: "||item["Category"]||", Subcategory: "||item["Subcategory"]||", Value = "||Char(item["Value"]))
)
);
);Sortir:
Category: A
- Subcategory: aa
- Category: A, Subcategory: aa, Value = 123
- Category: A, Subcategory: aa, Value = 124
- Subcategory: bb
- Category: A, Subcategory: bb, Value = 125
- Category: A, Subcategory: bb, Value = 124
- Subcategory: cc
- Category: A, Subcategory: cc, Value = 125
Category: B
- Subcategory: aa
- Category: B, Subcategory: aa, Value = 126
- Category: B, Subcategory: aa, Value = 127
- Subcategory: bb
- Category: B, Subcategory: bb, Value = 127
- Category: B, Subcategory: bb, Value = 126
- Subcategory: cc
- Category: B, Subcategory: cc, Value = 127
- Category: B, Subcategory: cc, Value = 127
- Category: B, Subcategory: cc, Value = 126Re : Comment parcourir une liste de tableaux associatifs regroupés par des valeurs clés spécifiques ?Même idée, reconstruisez votre liste en quelque chose de plus utilisable, puis faites-en ce dont vous avez besoin
Names Default To Here(1);
aalist = {["Category" => "A", "Subcategory" => "aa", "Value" => "123"], ["Category" => "A", "Subcategory" => "aa",
"Value" => "124"], ["Category" => "A", "Subcategory" => "bb", "Value" => "125"], ["Category" => "A", "Subcategory"
=> "bb", "Value" => "124"], ["Category" => "A", "Subcategory" => "cc", "Value" => "125"], ["Category" => "B",
"Subcategory" => "aa", "Value" => "126"], ["Category" => "B", "Subcategory" => "aa", "Value" => "127"], ["Category"
=> "B", "Subcategory" => "bb", "Value" => "127"], ["Category" => "B", "Subcategory" => "bb", "Value" => "126"], [
"Category" => "B", "Subcategory" => "cc", "Value" => "127"], ["Category" => "B", "Subcategory" => "cc", "Value" =>
"127"], ["Category" => "B", "Subcategory" => "cc", "Value" => "126"]};
aa = Associative Array();
For Each({aa_item}, aalist,
If(!Contains(aa, aa_item["Category"]),
aa[aa_item["Category"]] = Associative Array();
);
If(!Contains(aa[aa_item["Category"]], aa_item["Subcategory"]),
aa[aa_item["Category"]][aa_item["Subcategory"]] = {};
);
Insert Into(aa[aa_item["Category"]][aa_item["Subcategory"]], aa_item["Value"]);
);
For Each({{category, subcategory}}, aa,
Write("\!NCategory: ", category);
For Each({{subcategory_key, subcategory_values}}, subcategory,
Write("\!N\!t- Subcategory: ", subcategory_key);
For Each({value}, subcategory_values,
Write("\!N\!t\!t- Category: ", category, ", Subcategory: ", subcategory_key, ", Value = ", value);
);
);
);