条件中断或退出reactjs中的循环

时间:2018-04-09 20:56:06

标签: javascript reactjs

在我的源代码中,我希望根据某些条件来破坏嵌套循环。我使用了返回和休息但不起作用。即使是休息,我也会遇到构建错误。有人能帮我吗。我的代码如下:

updateUserData(event,id){
    alert(id);
    var elementName=event.target.name;
    alert('element name = '+elementName);
    var userData=this.state.user_data;
    var found=false;
    userData.sports.map(function(sport){
      sport.ticket_detail.map(function(ticket){
        alert(ticket.id);
        if(ticket.id==id){
          alert('matched');
          ticket.ticket.map(function(element){
            alert(element.paramname)
            var str1 = element.paramname;
            var str2 = event.target.name;
            if(element.paramname==event.target.name){
                alert('sport = '+sport.name+' category = '+ticket.cat+' id = '+ticket.id+' paramname = '+element.paramname);
                element.value='hitesh';
                found=true;
                return;
            }
          })
        }
          if(found){
            alert('found1');
            return;
          }
      })
      if(found){
        alert('found2');
        return;
      }
    })
    console.log('updated='+JSON.stringify(userData));
    this.setState({user_data:userData});
  }

2 个答案:

答案 0 :(得分:0)

似乎您不打算映射任何值。所以,就像这样简单的for循环:

updateUserData(event,id){
    alert(id);
    var elementName=event.target.name;
    alert('element name = '+elementName);
    var userData=this.state.user_data;

    for(let sport in userData.sports){
        for(let ticket in sport.ticket_detail){
            alert(ticket.id);
            if(ticket.id==id){
                alert('matched');
                for(let element in ticket.ticket){
                    alert(element.paramname);
                    var str1 = element.paramname;
                    var str2 = event.target.name;
                    if(element.paramname==event.target.name){
                        alert('sport = '+sport.name+' category = '+ticket.cat+' id = '+ticket.id+' paramname = '+element.paramname);
                        element.value='hitesh';
                        console.log('updated='+JSON.stringify(userData));
                        this.setState({user_data:userData});
                        return;
                    }
                }
            }
        }
    }
}

或者您可以使用Array.prototype.some来测试数组中是否至少有一个元素通过了由提供的函数实现的测试。

答案 1 :(得分:0)

如果我运行你的代码,我会收到此错误,因为它是nacaught ReferenceError:sport未定义     在RegisterWindow.updateUserData(index.js:51705)如果我像这样添加var(var user in userData.sports)然后我跳过那个错误,然后警告(sport.name);产生未定义的。

JSON是
{"total_tickets":3,"total_amount":2124,"htmlid_counter":3,"sports":[{"name":"Badminton","ticket_detail":[{"cat":"Men Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":0},{"cat":"Men Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":1},{"cat":"Men Singles","formid":102,"ticket":[{"label":"Name:","paramname":"name","type":"text","value":""},{"label":"Email:","paramname":"email","type":"text","value":""},{"label":"Phone:","paramname":"phone","type":"text","value":""}],"id":2}]},{"name":"Carrom","ticket_detail":[]}],"tournament_id":1}