Ok, here is how I did it in 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"]))
)
);
);
Output:
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 = 126