如何通过图形层中的 id 删除图形

How can i remove a graphic by id in the graphic layer

我如何通过图形中的 id 删除图形 layer.I 我正在使用 esri-javascript-api.Hope 任何答案。

假设 graphicLayer 是地图图形的图层,id 属性被命名为 "id" 并且您想要删除具有 id 0;

的图形
var graphicLayer = map.graphics;
var idAttribute = "id";
var idValue = 0;

var toBeRemoved = graphicLayer.graphics.filter(function(graphic) {
  return graphic.attributes[idAttribute] == idValue;
})[0];

graphicLayer.remove(toBeRemoved);

您可以创建一个函数来批量执行此操作

function removeGraphicById(graphicLayer, idAttribute, idValue) {
    var toBeRemoved = graphicLayer.graphics.filter(function(graphic) {
      return graphic.attributes[idAttribute] == idValue;
    })[0];

    graphicLayer.remove(toBeRemoved);
};

var idsToDelete = [0, 1, 2, 3, 4];

idsToDelete.forEach(function(id) {
  //using map.graphics layer for instance
  removeGraphicById(map.graphics, "id", id);
});