Group elements by view

So I was trying to create a script (attached) where the user selects a specific view, and the script takes some annotation elements and puts them in a group named after the view name.

The script works fine if the user selects a single view, but I want to extend it’s functionality to perform the same action if the user selects multiple views but it doesn’t work the way I want it to be, I think Dynamo is trying to group all elements from different views into one group.

Is there a way to modify this script to make dynamo filter elements by view, then place elements from one view in one group named after this view ?

Look into Data-Shapes and allow the user to select views that way. Then just loop the list of views with what you already have grouping and naming.