for 循环导致无法设置未定义的属性“0" [英] for-loop causes Cannot set property '0' of undefined

查看:78
本文介绍了for 循环导致无法设置未定义的属性“0"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在下面发布的代码中,我想遍历数组的内容

in the below posted code i would like to loop through the contents of the array

features[0]["values_"]["geometry"]["flatCoordinates"]

对于for循环之外的所有控制台语句,它们会被打印出来并包含数据.但是,当我运行应用程序时,我收到以下错误

for all the console statements that are outside the for-loop, they gets printed and they contain data.however, when i run the App, i receive the following error

ERROR TypeError: Cannot set property '0' of undefined

为了解决这个问题,我注释掉了 for 循环内的前两行,并打印了变量i"的值.只有当我激活循环内的前两行时,才会收到错误消息.请告诉我如何解决此错误

to solve this issue i commented out the first two lines inside the for-loop, and the values of the variable 'i' gets printed. only when i acrivate the first two lines inside the loop the error message is received. please let me know how to fix this error

代码:

private renderSelectedSite(){
    var features = (new GeoJSON()).readFeatures(this.selectedSite.geometry);
    console.log("this.selectedSite.geometry: ",this.selectedSite.geometry)
    console.log("features ",features)
    console.log("features ",features[0]["values_"]["geometry"]["flatCoordinates"])
    console.log("features ",features[0]["values_"]["geometry"]["flatCoordinates"].length)//84
    console.log("features ",features[0]["values_"]["geometry"]["flatCoordinates"][0])//736437.816446109
    var points: any[];
    for (var i = 0; i < features[0]["values_"]["geometry"]["flatCoordinates"].length; i++) {
        var e = features[0]["values_"]["geometry"]["flatCoordinates"][i];
        points[i] = transform(e,'EPSG:4326','EPSG:3857');
        console.log("points[i] ",i)
    }

推荐答案

需要初始化变量points.

var points: any[] = [];


以下是我建议的一些 QOL 改进:


Here are some QOL improvements I'd suggest:

  1. 使用 const/let 而不是 var.请参阅此处了解规范差异.
  2. 使用Array#map 而不是 for.
  1. Use const/let instead of var. See here for the canonical difference.
  2. Use Array#map instead of for.

private renderSelectedSite(){
  const features = (new GeoJSON()).readFeatures(this.selectedSite.geometry);
  let points: any[] = features[0]["values_"]["geometry"]["flatCoordinates"].map(e => 
    transform(e,'EPSG:4326','EPSG:3857')
  );
}

  1. 正如@Cerbrus 在评论中提到的那样,我还会帮助进行类型检查以定义对象的类型(例如,使用 TS 接口)并使用点访问而不是使用括号表示法来访问属性.
  2. 立>

更新:登录Array#map

将回调括在花括号中,如果您有多个语句,则显式返回响应.

Update: logging inside Array#map

Enclose the callback in curly braces and explicitly return the response if you have more than one statement.

let points: any[] = features[0]["values_"]["geometry"]["flatCoordinates"].map(e => {
  console.log(transform(e,'EPSG:4326','EPSG:3857'));
  return transform(e,'EPSG:4326','EPSG:3857');
});

console.log(points);


更新:重复问题

希望这可以解决此处这里也是.请停止发布针对同一问题的重复问题.


Update: Duplicate questions

Hopefully this solves the issue from here and here too. Please stop posting duplicate questions for the same issue.

如果您确定平面坐标始终只包含 8 个条目,则可以毫不费力地构建排列的坐标.

If you're sure the flat co-ordinates will always only contain 8 entries, you could construct the arranged co-ordinates without any fuss.

const flatCoords = features[0]["values_"]["geometry"]["flatCoordinates"];
const coords = [
  [flatCoords[0], flatCoords[1]],
  [flatCoords[2], flatCoords[3]],
  [flatCoords[4], flatCoords[5]],
  [flatCoords[6], flatCoords[7]],
];

然后你可以迭代数组并使用 Array#map 获取点.

Then you could iterate the array and fetch the points using Array#map.

let points = coords.map(coord => transform(coord,'EPSG:4326','EPSG:3857'));

更新:平面坐标的动态数量

您可以编写一个快速例程来将平面坐标转换为所需的格式

Update: dynamic number of flat co-ordinates

You could write a quick routine to convert the flat-coords to the desired format

toCoords(flatCoords: any) {
  let coords = [];
  for (let i = 0; i < flatCoords.length; i += 2) {
    coords.push([flatCoords[i], flatCoords[i+1]])
  }
  return coords;
}

const coords = this.toCoords(features[0]["values_"]["geometry"]["flatCoordinates"];
let points = coords.map(coord => transform(coord,'EPSG:4326','EPSG:3857'));

这篇关于for 循环导致无法设置未定义的属性“0"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆