Removing is invalid

Q1:

 var sketchNode = myDiagram.findNodesByExample({ key: "sketch" });
 console.log("sketchNode.count:"+sketchNode.count);//if the sketchNode.count is 2.
 var it = sketchNode.iterator;
 while (it.next()) {
      console.log(it.key + "--- " + it.value);
 }
myDiagram_${diagramId}_${diagramType}.startTransaction("removesketchNodes");
myDiagram_${diagramId}_${diagramType}.model.removeNodeDataCollection(sketchNode);//removing is invalid ,why???
myDiagram_${diagramId}_${diagramType}.commitTransaction("removesketchNodes");

the results of console.log();

First, I want to find the nodes that it’s key is “sketch” on the myDiagram.
Then,I want to remove the nodes on the myDiagram.
What Would l Do?

Q2:

var nodeLength = myDiagram.model.nodeDataArray.length;
			console.log("nodeLength:"+nodeLength);
			for(var k=0;k<nodeLength;k++){
				var sketchNode = myDiagram.model.nodeDataArray[k].key;
				if(sketchNode=="sketch"){
					var nodedata = myDiagram.model.nodeDataArray[k];
					myDiagram.startTransaction("removesketchNodes");
					myDiagram.model.removeNodeData(nodedata);
					myDiagram.commitTransaction("removesketchNodes");
				}
			}

When I delete it one by one, and make a mistake, what is the reason?

The problem with your code is that Model.removeNodeDataCollection takes an Array or a go.Iterable of model data objects. But Diagram.findNodesByExample returns an Iterator of Nodes, not node data.

If “key” is actually the value of Model.nodeKeyProperty, then I do not see how you could find more than one node for that key. Maybe if you used a RegExp or if you were checking a different property.

As it is, assuming there can be only one such node:

myDiagram.commit(function(d) {
  d.remove(d.findNodeForKey("sketch"));
}, "removeSketchNode");

Or you could do the same thing with model methods, operating on model data.

For Q2, you are modifying a collection (in this case an Array) while you are iterating over it. The behavior for that operation is not defined.

  mySketch.model.nodeDataArray = [ 
	{
		key : "sketch",
		nodeId : "1",
		category : "lable",
		......
	},{
		key : "sketch",
		nodeId : "2",
		category : "circle",
		......
    },{
    	key : "sketch",
		nodeId : "3",
		category : "rectangle",
		......
    },{
    	key : "sketch",
		nodeId : "4",
		category : "triangle",
		......
    },{
    	key : "sketch",
		nodeId : "5",
		category : "arrow",
		......
    }];

Q1:I set the nodeKeyProperty is"nodeId";
Q2:I am modifying a collection (in this case an Array) while I am iterating over it. The behavior for that operation is not defined.I don’t know what is the right thing to do.

myDiagram.commit(function(d) {
  d.removeParts(d.findNodesByExample({ key: "sketch" }), false);
}, "removeSketchNodes");