How do I group an Array by their partial common unique identifier?

I have a JSON that I am working with. The JSON is an Array of Objects.

It’s much longer than this but here’s a sample of it:

0: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.2.c", element_name: "Public Speaking", scale_id: "CX", …}
1: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.2.f", element_name: "Telephone", scale_id: "CX", …}
2: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.2.h", element_name: "Electronic Mail", scale_id: "CX", …}
3: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.2.j", element_name: "Letters and Memos", scale_id: "CX", …}
4: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.2.l", element_name: "Face-to-Face Discussions", scale_id: "CX", …}
5: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.a.4", element_name: "Contact With Others", scale_id: "CX", …}
6: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.b.1.e", element_name: "Work With Work Group or Team", scale_id: "CX", …}
7: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.b.1.f", element_name: "Deal With External Customers", scale_id: "CX", …}
8: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.b.1.g", element_name: "Coordinate or Lead Others", scale_id: "CX", …}
9: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.c.1", element_name: "Responsible for Others' Health and Safety", scale_id: "CX", …}
10: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.c.2", element_name: "Responsibility for Outcomes and Results", scale_id: "CX", …}
11: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.d.1", element_name: "Frequency of Conflict Situations", scale_id: "CX", …}
12: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.d.2", element_name: "Deal With Unpleasant or Angry People", scale_id: "CX", …}
13: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.1.d.3", element_name: "Deal With Physically Aggressive People", scale_id: "CX", …}
14: {onetsoc_code: "17-2051.00", title: "Civil Engineers", element_id: "4.C.2.a.1.a", element_name: "Indoors, Environmentally Controlled", scale_id: "CX", …}

You’ll see that there’s an element_id that has a unique identifier in it. Ultimately there’s some common ancestors to these. For instance, several have 4.C.1 as a common code, or even 4.C.1.a .

Ultimately I only need iterated Arrays of each of the Arrays that share an ancestor of the element_id up to the 3rd character. Even further I only need those new Arrays to contain the property data_value (not shown in list cause I can’t seem to copy the full array) because I’ll be averaging those out afterwards.

The part where I’m stuck is in how to group these by a common ancestor without having to create a separate list of the all the ancestors. Any help would be greatly appreciated!