2016-05-18 42 views
-6

如何使用foreach进行此操作?我想显示所有的名字。Foreach在Typescript中不起作用

test: [{ 
    id: "1", 
    name: "Tname1" 
}, { 
    id: "2", 
    name: "Tname2" 
}] 

它不工作:

this.test.forEach(function(s){ 
    console.log(s.name); 
}); 
+3

请阅读[this(how to ask)](http://stackoverflow.com/help/how-to-ask)和[this(mcve)](http:// stackoverflow .com/help/mcve),然后询问, 这些将帮助您从社区获得更多更好的答案。此外,当你提出一个显示负面因素鼹鼠努力的问题时,没有人会打扰。 – Bonatti

回答

4

我认为你需要这样的:

test = [ 
    { 
     id: "1", 
     name: "Tname1" 
    }, 
    { 
     id: "2", 
     name: "Tname2" 
    } 
]; 

在打字稿:是定义一个元素的类型。例如:

test: any[] = [ ... ];