简体   繁体   English

TypeScript:获取表中的值时未定义的值

[英]TypeScript:Undefined value when get value in table

How can I get value of table in typeScript, ? 如何在typeScript中获取表的值? console.log(jsonUsers)的结果

myComponent.ts : myComponent.ts:

let jsonUsers = [];
getUser() {

// When I do this console ==> I get response on the picture
 console.log(this.jsonUsers );

// Now I want to get value of code and label but it is not worked 
 console.log(jsonUsers.length); // ==> 0
 for (var index = 0; index < this.jsonUsers .length; index++) {
      var element = this.jsonUsers [0];
      console.log(element.code);
 }

mycode的

looks like that array has no elements on it, those that you see in the console's output are object's keys. 看起来该数组上没有任何元素,您在控制台输出中看到的是对象的键。 Try something like this, and let us know what you see 试试这样,让我们​​知道您所看到的

let jsonUsers = [];
getUser() {
    const keys = Object.keys(this.jsonUsers);
    keys.forEach((k, i) => {
        const element = this.jsonUsers[k];
        console.log(element.code)
    })
}

or another option could be using for...in 或其他选项可能for...in

let jsonUsers = [];
getUser() {
    for (let k in this.jsonUsers) {
        const element = this.jsonUsers[k];
        console.log(element.code)
    }
}

note that this.jsonUsers is a different variable than jsonUsers , keep that in mind 请注意, this.jsonUsers是与jsonUsers不同的变量,请记住

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM