从递归函数中断并返回JavaScript中的值



我已经写下了这样的JavaScript函数。但是我想要当Cretain条件符合该功能时不会执行该功能,这意味着它将破坏并返回一个true false,例如状态。我的代码就像

 var ActionAttributes = function (data)
    {
        var status = true;
        var attrKey = data.AttributeKey();
        //Condition to exit
        if (attrKey==''||attrKey==null)
        {
            status = false;
            return false;
        }
        for (var i = 0; i < data.Children().length; i++)
        {
            var childData = data.Children()[i];
            ActionAttributes(childData);
        }
        return status;
    }

您需要在for循环中的断裂状态。您只是在调用它,处理返回的status

var ActionAttributes = function(data) {
    var status = true;
    var attrKey = data.AttributeKey();
    //Condition to exit
    if (attrKey == '' || attrKey == null) {
        status = false;
        return false;
    }
    for (var i = 0; i < data.Children().length; i++) {
        var childData = data.Children()[i];
        //You need to break loop here
        //Add appropriate condition here
        if (ActionAttributes(childData) == false) {
            return false;
        }
    }
    return status;
}

好吧,递归开始不是很有用。

您将循环内部的ActionAttributes递归,但从未处理返回的状态。因此,除非出口条件在第一个对象符合,否

您将返回从ActionAttributes到状态中存储,然后在错误后立即脱离循环。

最新更新