I am trying to delete all graphics within a polygon, but the following code only removes the first graphic. Any suggestions?
fieldGeom is a valid polygon. It removes one point graphic, but then exits loop and does not remove a second point graphic.
Thanks!
this.pointGraphics.graphics.forEach(function (g) { | |
for (var x = 0; x < fieldIds.length; x++) { | |
fieldGeom = that.getFieldGeometry(fieldIds | |
if (fieldGeom.contains(g.geometry) === true) { | |
that.pointGraphics.remove(g); | |
} | |
} | |
}); |
Solved! Go to Solution.
Jerry,
When looping though an array to delete objects from that array you always need to loop in reverse.
for (var d = this.pointGraphics.graphics.length - 1; d-- >= 0;) {
var dpnt = this.pointGraphics.graphics
; for (var x = 0; x < fieldIds.length; x++) {
fieldGeom = that.getFieldGeometry(fieldIds
); if (fieldGeom.contains(dpnt.geometry) === true) {
that.pointGraphics.remove(dpnt);
}
}
}
Jerry,
When looping though an array to delete objects from that array you always need to loop in reverse.
for (var d = this.pointGraphics.graphics.length - 1; d-- >= 0;) {
var dpnt = this.pointGraphics.graphics
; for (var x = 0; x < fieldIds.length; x++) {
fieldGeom = that.getFieldGeometry(fieldIds
); if (fieldGeom.contains(dpnt.geometry) === true) {
that.pointGraphics.remove(dpnt);
}
}
}