【问题标题】:error while compiling in typescript在打字稿中编译时出错
【发布时间】:2018-02-18 03:34:23
【问题描述】:
private seats: number[][] = [[10],[10]];
        for(let i:number=0; i < 10; i++)
        for(let j:number=0;j<10;j++)
            {
                this.seats[i][j] = '_';
            }

这给了我一个错误

this.seats[i][j] = '_" TypeError:无法设置未定义的属性“0” 怎么解决?

【问题讨论】:

  • 我已经完成了,但我找不到任何东西...我收到一个错误 this.seats[i][j] = '-'; ^ 类型错误:无法设置未定义的属性“0” @jonrsharpe
  • 然后给出一个minimal reproducible example 那个尝试。看起来您创建了一个空数组作为seats,在这种情况下seats[i] 将是undefined
  • 你能帮我编码一下吗? @jonrsharpe 会很有帮助。我想要 typescript 中的上述代码,它在 java 中
  • 否; SO 不是代码编写服务。这也不是 JavaScript;如果是的话,它已经在 TypeScript 中工作了,TypeScript 是 JS 的超集。

标签: javascript typescript


【解决方案1】:

首先,您要创建一个二维 number 数组。所以你不能分配_,因为它是一个字符串。其次,您没有初始化辅助数组

试试下面的代码:

private seats: number[][] = []; // 2d array is just an array

for(let i:number=0; i < 10; i++) {
    this.seats[i] = []; // you need to init the inner array

    for(let j:number=0; j<10; j++) {
        this.seats[i][j] = i;
    }
}

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-06-26
    • 2020-01-17
    • 2018-04-20
    • 2016-08-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-09-09
    相关资源
    最近更新 更多