Trying to loop through all graphics in view and delete ones that have geometry = "polyline"
Solved! Go to Solution.
When you remove items from an array using a loop, you have to loop through the array in reverse, removing the last item.
const graphicsArr = view.graphics;
if (graphicsArr.length > 0) {
for (let i = graphicsArr.length - 1; i >= 0; i--) {
console.log(graphicsArr[i])
if (graphicsArr[i].geometry.type == "polyline") {
view.graphics.remove(graphicsArr[i])
}
}
}
Could it be that you are iterating over the graphicsArr, but are removing the item from the view.graphics directly?
When you remove items from an array using a loop, you have to loop through the array in reverse, removing the last item.
const graphicsArr = view.graphics;
if (graphicsArr.length > 0) {
for (let i = graphicsArr.length - 1; i >= 0; i--) {
console.log(graphicsArr[i])
if (graphicsArr[i].geometry.type == "polyline") {
view.graphics.remove(graphicsArr[i])
}
}
}
Wow, thank you. I ended up just creating graphics layers and clearing out all graphics associated with a given layer when I added a different layer (rather than doing it by geometry type). But this solution will be super valuable in the future. Thanks, Ken!