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]);
}
}
}
}
}
以上代码成功运行 并且测试是console.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");
}
}
}
});
});
对于电子商务应用程序,我正在将产品从高价过滤到 从低到高,所以我做了这段代码这是整个代码 谁想看到任何人有答案??注意:当我使如果 条件像第一个例子一样有效,所以问题出在 自言自语
答案 0 :(得分:0)
我阅读了您的评论
是的,它正在完美地安慰它
我确定原因是
if(x === total.length - 1){
total.push(found[i]);
}
如果x === total.length-1,则将新数据[found [i]]推到[total],这将更改总长度,然后x ++
如果遇到这种情况,此步骤将每次重复,它将无限循环,并且永远不会在if语句中运行,因为found [i]不变=>您的浏览器已停止。