&引用;如果;及;否则,如果;这两个条件都在filter函数中执行(Angular-8,JavaScript)

&引用;如果;及;否则,如果;这两个条件都在filter函数中执行(Angular-8,JavaScript),javascript,angular,Javascript,Angular,“if”和“else-if”条件都在filter函数中执行。当我试图执行下面的程序时,我得到的输出是 this.users.filter((hero)=>{ if(hero.name==this.profileForm.value.name&&hero.password==this.profileForm.value.password){ this.router.navigate(['/dashboard']); console.log(“如果需要,请登录”) }else if(hero.na

“if”和“else-if”条件都在filter函数中执行。当我试图执行下面的程序时,我得到的输出是

this.users.filter((hero)=>{
if(hero.name==this.profileForm.value.name&&hero.password==this.profileForm.value.password){
this.router.navigate(['/dashboard']);
console.log(“如果需要,请登录”)
}else if(hero.name!==this.profileForm.value.name){
console.log(“如果需要,请登录”)
this.isValid=false;
}
谁能帮我一下代码有什么问题吗

从'@angular/core'导入{Component,OnInit};
从'@angular/forms'导入{FormGroup,FormControl,Validators};
进口{
激活,路由器,
激活的路由快照,
路由器状态快照
}来自“@angular/router”;
@组成部分({
选择器:“应用程序登录”,
templateUrl:'./login.component.html',
样式URL:['./login.component.css']
})
导出类LoginComponent实现OnInit{
构造函数(专用路由器:路由器){}
恩戈尼尼特(){
}
isValid:boolean=true;
使用者=[{
名称:“user1”,
密码:“password1”
},
{
名称:“user2”,
密码:“password2”
}
]
profileForm=新表单组({
名称:新FormControl(“”,需要验证程序),
密码:新FormControl(“”,[Validators.minLength(6),Validators.required]),
});
onSubmit(){
this.users.filter((英雄)=>{
if(hero.name==this.profileForm.value.name&&hero.password==this.profileForm.value.password){
this.router.navigate(['/dashboard']);
console.log(“如果需要,请登录”)
}else if(hero.name!==this.profileForm.value.name){
console.log(“如果需要,请登录”)
this.isValid=false;
}
}); 
}
}

欢迎来到时间表跟踪器
请输入用户名。。!
请输入有效密码。。!
登录

请输入有效的用户名和密码


这里不需要
过滤器。
.filter
对每个值进行迭代。对于某些值,满足一个条件,对于其他值,满足另一个条件,因此
if
else
似乎都在执行

您需要做的是这样的事情:

 let heroFound = false;

  for(let hero of this.users){
   if(hero.name === this.profileForm.value.name && hero.password === 
  this.profileForm.value.password){
       heroFound = true;
       break;

    }

  }

  if(heroFound){
   this.router.navigate(['/dashboard']);
  }else{
    this.isValid=false;
  }

数组
users
有两个元素。一个元素满足
if
条件,另一个元素满足
else if
。这就是为什么会看到这两个元素都打印出来。为什么要在
过滤器()中执行
路由器.导航()