数组的foreach

Foreach for array

我有一个像这样填充的数组

var list = [];

featureLayer.queryFeatures(querySnTR)
            .then((result) => {
                                
                   result.attachmentInfos.forEach((x) => {
                   list.push(uriString + "/" + x.id);
                  });
                });
console.log("list", list);

我用 console.log 打印出 list 并且里面有 returns 值。

之后,我执行 foreach 遍历所有内部元素并为每个元素创建一个 div。问题是,它甚至没有进入 foreach 函数。

list.forEach((x) => {
    console.log("CL", list);
    console.log("x element", x);

    var image = document.createElement("img");
    image.src = x;
    image.className = "queryImg";
    document.getElementById("queryResults").appendChild(image);
  });

就此而言,它不会打印出 CLx element。 关于原因有什么想法吗?

完整原代码,供参考

startup: function () {

                var _that = this;
                _this = _that;

                this.map.on("click", function (e) {
                    _this.map.graphics.clear();

                    identifyTask = new IdentifyTask("https://server/arcgis/rest/services/MUNICIPALITY_BUNDLE/ZK_KATASTAR_NA_ZELENILO/MapServer");

                    identifyParams = new IdentifyParameters();
                    identifyParams.tolerance = 10;
                    identifyParams.returnGeometry = true;
                    identifyParams.layerIds = [1];
                    identifyParams.layerOption = IdentifyParameters.LAYER_OPTION_ALL;
                    identifyParams.width = _this.map.width;
                    identifyParams.height = _this.map.height;
                    identifyParams.spatialReference = _this.map.spatialReference;

                    identifyParams.geometry = e.mapPoint;
                    identifyParams.mapExtent = _this.map.extent;

                    identifyTask.execute(identifyParams).then(function (data) {
                        objId = data[0].feature.attributes.objectid;

                        const querySnTR = {
                            where: "1 = 1",
                            outFields: ["*"]
                        };

                        var uriString = "https://server/arcgis/rest/services/MUNICIPALITY_BUNDLE/ZK_KATASTAR_NA_ZELENILO/MapServer/101/" + objId + "/attachments";
                        var featureLayer = new esri.layers.FeatureLayer(uriString);

                        featureLayer.queryFeatures(querySnTR)
                            .then((result) => {

                                result.attachmentInfos.forEach((x) => {
                                    list.push(uriString + "/" + x.id);
                                });
                            });

                        const myFunction = async () => {
                            const { attachmentInfos } = await featureLayer.queryFeatures(querySnTR);
                            const list = attachmentInfos.map(({ id }) => `${uriString}/${id}`);
                            console.log("list", list);
                            list.forEach((x) => {
                                var image = document.createElement("img");
                                image.src = x;
                                image.className = "queryImg";
                                document.getElementById("queryResults").appendChild(image);
                            });
                        };
                    });
                });
            }

这是有关控制台工作方式的技巧。 当您执行日志时,列表是空的(100% 确定),因为您正在异步填充它。但是控制台有对它的引用,它会在之后打印它。 这就是为什么你的列表是空的。您需要在这里处理异步。您可以使用 async/await 方法或使用 promises,这将取决于您的其余代码,这是一个如何使用异步函数执行此操作的示例(并将其重写为现代 javascript) :


const myFunction = async () => {
  const {attachmentInfos} = await featureLayer.queryFeatures(querySnTR);
  const list = attachmentInfos.map(({id}) => `${uriString}/${id}`);
  console.log("list", list);
  list.forEach((x) => {
    // put your code here
  });
};

已编辑: 现在您已经共享了所有代码,您可以简单地执行以下操作:

featureLayer.queryFeatures(querySnTR)
  .then((result) => {                              
      result.attachmentInfos.forEach((attachmentInfo) => {
        var x = uriString + "/" + attachmentInfo.id
        var image = document.createElement("img");
        image.src = x;
        image.className = "queryImg";
        document.getElementById("queryResults").appendChild(image);
     });
});

我建议你也给变量起有意义的名字,不是x而是attachmentInfo,等等...