Ways to group multiple items under categories

I have a list of categories and items to include on the same row as those categories, but they are in a separate row and marked by numbers in another row. See uploaded .ods file for what I’m talking about.

Is there a way to check the number in the E column against the number in the A column and put all items for the corresponding D column into a cell together?
I was starting off trying something like =IF($E2=A$2, $D2, 0) and dragging it down and to the right.

Also considering using something along the lines of =INDIRECT(ADDRESS(E2+1; 2))

sort example.ods

With the function Textjoin()

=TEXTJOIN(", ",1,IF($E$2:$E$13=G2,$D$2:$D$13,""))

(Matrix formula ► End input with Ctrl + Shift + Enter)

16162032288800007.ods


If this answers your question, please click on the circle with the tick (✔) on the left (the point is highlighted in green and the answer is highlighted in color) so that other users can see that the question has been answered.

Great! That does the trick. I never knew about that function.