Deleting and Iterating

Hello,

My main goal for this logic is to delete a node. I am deleting the node but getting a error in the console:

Uncaught Error: Collection was modified during iteration: Set(Part)#246
Perhaps you should iterate over a copy of the collection,
or you could collect items to be removed from the collection after the iteration.

This is my logic:

                    var savedNode;
                               for (var it = myDiagram.nodes.iterator; it.next(); ) {
                                   while (it.next()) {
                                       console.log(it.value)
                                       if (it.value instanceof go.Node ) {
                                           if (it.value === group){
                                                savedNode = it.value;
                                            } 
                                        }
                                   }
                              } 
                               myDiagram.remove(savedNode)
                               myDiagram.removeParts(savedNode.findLinksConnected())

What am I doing wrong?

I figured it out! Thank you!!

Why do you have nested loops?

By default deleting a Node by calling Diagram.remove will also delete from the diagram all of the Links that connect with that Node.

If you are trying to delete all nodes from a diagram, just call Diagram | GoJS API on the Diagram.nodes collection: myDiagram.removeParts(myDiagram.nodes)
Within a transaction, of course.

Thank you for responding. I removed the while loop and removed the myDiagram.removeParts() function. I created a manual delete function because of what I needed to be done. This is only a snippet of it. But it works now. Thank you for your help!