提问者:小点点

在for循环内部未执行If语句


var total = [];
for(var i = 0; i < found.length; i++){
    if(total.length === 0){
            total.push(found[i]);
    }else{
        for(var x = 0; x < total.length; x++){
            if(total[x].price < found[i].price){//Here
                total.splice(x, 0, found[i]);
                console.log("Test");
                break;
            }else{
                 console.log("Testss");
                if(x === total.length - 1){
                    total.push(found[i]);
                }
            }
        }
    }
}

上面的代码工作成功,test是consoled.log

for(var i = 0; i < found.length; i++){
    if(total.length === 0){
            total.push(found[i]);
    }else{
        for(var x = 0; x < total.length; x++){
            if(total[x].price > found[i].price){ //The sign changed here
                total.splice(x, 0, found[i]);
                console.log("Test");
                break;
            }else{
                 console.log("Testss");
                if(x === total.length - 1){
                    total.push(found[i]);
                }
            }
        }
    }
}

上面的不起作用我也找不到原因,这只是一个标志注:代码在其他函数中不在同一个函数中有人能帮忙吗?

整个代码:

  router.get("/shop", function(req, res){
        const range1    = parseInt(req.cookies.pricerange1);
        const range2    = parseInt(req.cookies.pricerange2);
        var category;
        var findattr = {};
        if(typeof req.query.search != 'undefined'){
            findattr = {name: req.query.search};
            req.cookies.category = -1;
        }
        if(typeof req.query.search === 'undefined'){
            category = -1;
        }else{
            category  = parseInt(req.cookies.category);
        }
        Product.find(findattr, function(err, found){
            if(err){
                req.flash("error", "An Error has occured, Please try again later.");
                res.redirect("/shop");
            }else{
                if(found.length === 0){
                    req.flash("error", "No Products were found, Try again later.");
                    return res.redirect("/");
                }
                var minvalue = 100000000000000000;
                var maxvalue = 0;
                var total = [];
                if(req.cookies.pricerange1 === undefined && req.cookies.pricerange2 === undefined){
                    for(var i = 0; i < found.length; i++){
                        if(found[i].price < minvalue){
                            if(category === found[i].category || category === -1){
                                minvalue = found[i].price;
                            }
                        }
                        if(found[i].price > maxvalue){
                            if(category === found[i].category || category === -1){
                                maxvalue = found[i].price;
                            }
                        }                    
                        if(category === found[i].category || category === -1){
                            if(req.cookies.sort === '0' || typeof req.cookies.sort === 'undefined'){
                                total.push(found[i]);
                            }
                            if(req.cookies.sort === '1'){
                                total.push(found[i]);
                            }
                            if(req.cookies.sort === '2'){
                                if(total.length === 0){
                                    total.push(found[i]);
                                }else{
                                    for(var x = 0; x < total.length; x++){
                                        if(total[x].price < found[i].price){//Here working
                                            total.splice(x, 0, found[i]);
                                            break;
                                        }else{
                                            if(x === total.length - 1){
                                                total.push(found[i]);
                                            }
                                        }
                                    }
                                }
                            }
                            if(req.cookies.sort === '3'){
                                if(total.length === 0){
                                    total.push(found[i]);
                                }else{
                                    for(var x = 0; x < total.length; x++){
                                        if(total[x].price > found[i].price){//Here is the code not working
                                            total.splice(x, 0, found[i]);
                                            console.log("Test");
                                            break;
                                        }else{
                                            if(x === total.length - 1){
                                                total.push(found[i]);
                                            }
                                        }
                                    }
                                }
                            }else if(req.cookies.sort === '4'){
                                total.push(found[i]);
                            }
                        }
                        if(i === found.length - 1){
                            res.render("products/shop", {products: found, product: total, pageinfo:"Shop", 
                            minvalue: minvalue, maxvalue: maxvalue});
                        }
                    }
                }else{
                    if(range1 != "NaN" && range2 != "NaN"){
                        var total = [];
                        for(var i = 0; i < found.length; i++){
                            if(found[i].price < minvalue){    
                                if(category === found[i].category || category === -1){                        
                                    minvalue = found[i].price;
                                }
                            }
                            if(found[i].price > maxvalue){
                                if(category === found[i].category || category === -1){
                                    maxvalue = found[i].price;
                                }
                            }
                            if(found[i].price >= range1 && found[i].price <= range2){                            
                                if(category === found[i].category || category === -1){
                                    total.push(found[i]);
                                }
                            }
                            if(i === found.length - 1){
                                if(range1 < minvalue){
                                    if(range2 > maxvalue){
                                        res.clearCookie('pricerange1');
                                        res.clearCookie('pricerange2');
                                        res.render("products/shop", {error: "Please Choose the price ranges again", products: found, product : total, pageinfo: "Shop", minvalue: minvalue, maxvalue: maxvalue});
                                    }else{
                                        res.render("products/shop", {products: found, product : total, pageinfo: "Shop", minvalue: minvalue, maxvalue: maxvalue, minvaluechosen: range1, maxvaluechosen: range2, range: true});
                                    }
                                }else{
                                    res.render("products/shop", {products: found, product : total, pageinfo: "Shop", minvalue: minvalue, maxvalue: maxvalue, minvaluechosen: range1, maxvaluechosen: range2, range: true});
                                }
                            }
                        }
                    }else{
                        res.clearCookie('pricerange1');
                        res.clearCookie('pricerange2');
                        req.flash("error", "Please Choose the price ranges again");
                        res.redirect("/shop");
                    }
                }
            }
        });
    });

它的电子商务应用程序,我是过滤的产品从高价格到低和从低到高,所以我做了这个代码,这是整个代码谁想看到谁有答案?? 注意:当我像第一个例子那样设置if条件时,它会起作用,所以问题出在语句本身


共1个答案

匿名用户

我读了你的评论

是的,它在无限地安慰着它

我肯定原因是

if(x === total.length - 1){
        total.push(found[i]);
  }

这个步骤每次都会重复,如果遇到这种情况,它将会无限循环,并且永远不会在if语句中运行,因为found[i]不是change=>; 您的浏览器已停止。