Select to view content in your preferred language

Delete Multiple Graphic

436
1
Jump to solution
11-16-2014 08:56 AM
JerryGarcia
Frequent Contributor

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);
                }
            }
        });
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

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);

                }

           }

        }

View solution in original post

0 Kudos
1 Reply
RobertScheitlin__GISP
MVP Emeritus

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);

                }

           }

        }

0 Kudos