简体   繁体   English

推送到数组后,JavaScript 停止循环执行

[英]JavaScript stops loop execution after push to array

I have a problem with simple script.我有一个简单的脚本问题。 I have a model called 'FeatureFlag' which is a tree structure with reference to parent and children.我有一个名为“FeatureFlag”的模型,它是一个参考父级和子级的树结构。 I have also object called TreeBuilder which makes a tree from a list of FeatureFlag elements.我还有一个名为 TreeBuilder 的对象,它从 FeatureFlag 元素列表中生成一棵树。

Here is my model:这是我的模型:

export class FeatureFlag {
    private _id: number;
    private _parent: FeatureFlag;
    private _children: FeatureFlag[] = [];

    set id(id:number) {
        this._id = id
    }

    set parent(parent:FeatureFlag) {
        this._parent = parent
    }

    get parent() : FeatureFlag {
        return this._parent
    } 

    set children(children:FeatureFlag[]) {
        this._children = children
    }

    get children() : FeatureFlag[] {
        return this._children
    }                       
}

and problematic method build() in TreeBuilder: TreeBuilder 中有问题的方法 build():

public build() {
    for(let element of this.data) { //this.data is an array of dict [{node,parent}]
        let found = this.findNode(element.parent) //searches in tree for node with id 'element.parent'

        if(found) { //if parent node has been found          
             //found.children.push(element.node) //add current node to its children
             console.log(found) //print found
         }           
    }
}

When line:当线:

found.children.push(element.node)

is commented, console prints all found parents.被评论,控制台打印所有找到的父母。 It is ok.没关系。 But when line is uncommented, console.log prints only once - a first found parent.但是当行被取消注释时,console.log 只打印一次 - 第一个找到的父级。 Why is that happening?为什么会这样?

Regards问候

Wierd! 怪! I would put a debugger statement before the push and then step through it to make sure it is not throwing an error that is silently getting caught. 我会在推送之前放置调试器语句,然后逐步执行它,以确保它不会引发被静默捕获的错误。 Also make sure that children.push does not have any unintended side affects. 还要确保children.push没有任何意外的副作用。

I suspect that children.push is somehow throwing, perhaps because children is not defined, but Angular is swallowing the error. 我怀疑children.push会以某种方式抛出,也许是因为未定义children,但是Angular吞没了错误。 Also, is findNode for sure returning a FeatureFlag and not something else? 另外,findNode是否确定返回FeatureFlag而不是其他东西?

I encountered similar issue:我遇到了类似的问题:

  var errors;
    let imput_email = $('#loginEmail').val().toUpperCase();
    if (!validateEmail(imput_email)){
        $( "#loginEmail + .invalid-feedback" ).addClass( "d-block" );
        console.log("I can see this in console");
        errors.push("Kiwi");
        console.log("NOTHING IN CONSOLE");
        alert("ALERT NOT WORKING EITHER");
    }
    if (!validatePass(imput_pass)){ // this part was working before push

What worked here is just initializing the errors variable:这里的工作只是初始化错误变量:

  var errors= [];

I can understand that if the array is empty and it has no information that it is an array it is one of those "silent errors".我可以理解,如果数组为空并且它没有任何信息表明它是一个数组,那么它就是那些“无声错误”之一。 Hope it helps someone despite the 4 years delay尽管延迟了 4 年,但希望它对某人有所帮助

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM