Get subset from list of matching ids

group_by can be handy if you’re grouping by something that isn’t unique, but here it puts an extra layer of wrapping around the resulting keyword lists:

[
  [[key1: "string", key2: "another string", key3: 0, id: 1]],
  [[key1: "string", key2: "another string", key3: 0, id: 1]],
  [[key1: "string", key2: "another string", key3: 0, id: 3]],
  [[key1: "string", key2: "another string", key3: 0, id: 2]],
  [[key1: "string", key2: "another string", key3: 0, id: 4]]
]

since the values returned by group_by are lists, not single elements.

Changing the last Enum.map to Enum.flat_map will handle that unwrapping, but since we KNOW that id values are unique from the problem statement, it seems wasteful to defensively program against duplicated values.

1 Like