groupByChildField method

Map<String, Map<String, dynamic>> groupByChildField(
  1. String field
)

Groups a map of season colors by their respective months.

This getter organizes the color data by their 'group' attribute, which represents a specific time period, such as a month. It restructures the class's internal map into a new map that is indexed by these group values.

Example:

Map<String, Map<String, dynamic>> colorData = {
  'color1': {'group': 'january', 'value': 'data'},
  'color2': {'group': 'january', 'value': 'data'},
  'color3': {'group': 'february', 'value': 'data'},
};

var groupedColors = colorData.getGroupedSeasonColors;
print(groupedColors);

Output:

{
  'january': {
    'color1': {'group': 'january', 'value': 'data'},
    'color2': {'group': 'january', 'value': 'data'}
  },
  'february': {
    'color3': {'group': 'february', 'value': 'data'}
  }
}

The output map will have keys that correspond to the groups (e.g., months) and each key maps to a nested map of color data that belongs to that group.

Returns: A Map<String, Map<String, dynamic>> where each key is a month and each value is a Map of color attributes for that specific month.

Implementation

Map<String, Map<String, dynamic>> groupByChildField(String field) {
  Map<String, Map<String, dynamic>> result = {};

  this.forEach((key, value) {
    final String group = value[field];
    if (!result.containsKey(group)) {
      result[group] = {};
    }

    result[group]![key] = value;
  });

  return result;
}