typescript 遍历angular2打字稿中的数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41503340/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-21 04:08:29  来源:igfitidea点击:

Iterating through an array in angular2 typescript

javascriptarraysangulartypescript

提问by Geoff

Am new to typescript and angular2 and learnining this and i have this issue. I have a form that i would like to show the results of an array of items but these items keep changing everytime so i dont now the actual index of the returned results.

我是 typescript 和 angular2 的新手,正在学习这个,我有这个问题。我有一个表格,我想显示一组项目的结果,但这些项目每次都在不断变化,所以我现在不知道返回结果的实际索引。

This is the case

这是这种情况

I have this form.html

我有这个 form.html

<span style="color:red">{{usererror.username}}  </span>
...other items here
<span style="color:red">{{usererror.password}}  </span>

Now in the component

现在在组件中

export class LoginComponent implements OnInit{
 public usererror={
    username:"",
    password:""
  }

  //this function is called after a post request
onLoginError(error:any){

let message= JSON.parse( error._body);
  console.log(error._body); //first log
  console.log(message)  //second log


}

}

As from the above function the first log returns an array of the form

从上面的函数中,第一个日志返回一个数组

[{"field":"username","message":"Username cannot be blank."},
  {"field":"password","message":"Password cannot be blank."}]

Now i would like to assign the username and password variables in the above usererror so that they can be displayed

现在我想在上面的 usererror 中分​​配用户名和密码变量,以便它们可以显示

So something like this

所以像这样

onLoginError(error:any){

let message= JSON.parse( error._body);
   let message= JSON.parse( error._body);

    for (var i=0; i<message.length; i++){
     this.usererror.message[i].field = message[i].message;  //this fails since the 
         //usererror doesnt have (message[i].field) property

     }
 }

sometimes the returned results dont have a username field and sometimes the password field. How do i go about this, am still new to angular2 and typescript

有时返回的结果没有用户名字段,有时没有密码字段。我该怎么做,我还是 angular2 和 typescript 的新手

回答by n00dl3

Use the square bracket notation (object["property"]) :

使用方括号符号 ( object["property"]) :

let messages= JSON.parse( error._body);
for (let message of messages){
    if(this.usererror[message.field])
      this.usererror[message.field]=message.message;
}

回答by Jason Evans

If all you require is to check whether this.usererror.message[i].fieldexists, before attempting to copy the message, then you can include an ifstatement like this:

如果您只需要this.usererror.message[i].field在尝试复制消息之前检查是否存在,那么您可以包含如下if语句:

for (var i=0; i<message.length; i++){
    if(this.usererror.message[i].field){
        this.usererror.message[i].field = message[i].message;
    }
}

If the field is undefined, as in it does not exist, then that ifstatement will return false.

如果该字段是undefined,因为它不存在,那么该if语句将返回false