当前位置: 首页 > 知识库问答 >
问题:

无法理解此“for”循环(重新编辑)的结果[重复]

汪信鸥
2023-03-14

我想做一个小程序,从数组中删除一些不需要的字符,如/n,/t,问题是它在部分工作。第三届和第五届):

程序:

var contenuespacemenu = $('.Espace-Menu').children('div').html(); //the array which contains characters i want to delete.  
var bannedcaractere = ["\n", "\t"];
var caractereinside = [];

  for (var i = 0; i < 50; i++) {
    for (var j = 0; j < (bannedcaractere.length); j++) {
      let test = contenuespacemenu[i] == bannedcaractere[j];
      if (test) {
        i = i + 1;
      } else {
        caractereinside[i] = contenuespacemenu[i];
      }
    }
  }


console.log(caractereinside);

它显示了这个结果:

一些不需要的字符被删除,但不是全部。。。指数3和5。

下面是将html标记的字符串内容逐字符放入数组“caractereinside”的主要代码

var contenuespacemenu = $('.Espace-Menu').children('div').html();
//var bannedcaractere = ["\n","\t"];
var caractereinside = [];



for (var i = 0; i < 50; i++) {

  caractereinside[i] = contenuespacemenu [i];
  }

console.log(caractereinside);

结果是:

如果我将"var ContenuesPacemenu"转换成一个规则数组,就像这样,它的工作原理是:

var contenuespacemenu = ["1","2","3","4","5","6","7","8"];
//var bannedcaractere = ["\n","\t"];
var caractereinside = [];



for (var i = 0; i < 50; i++) {

  caractereinside[i] = contenuespacemenu [i];
  }

console.log(caractereinside);

我明白了:

我还试着用一个中断来代替i=i 1,就像我被统计的那样,如下所示:

var contenuespacemenu = $('.Espace-Menu').children('div').html();
var bannedcaractere = ["\n","\t"];
var caractereinside = [];



for (var i = 0; i < 50; i++) {
  for (var j = 0; j < (bannedcaractere.length); j++) {
    let test = contenuespacemenu[i] == bannedcaractere[j];
    if (test) {
      break;
    }
    else {
      caractereinside[i] = contenuespacemenu[i];

  }

  }
}


console.log(caractereinside);

但它给了我这个:

最后,我再次尝试使用i=i 1,为var contentuespacemenu定义了一个数组,还更改了var bannedcharacter,它的工作方式与预期的一样:

var contenuespacemenu = ["1","2","3","4","5","6","7","8"];//the array which contains characters i want to delete.
var bannedcaractere = ["1", "2","3","4"];
var caractereinside = [];

  for (var i = 0; i < 50; i++) {
    for (var j = 0; j < (bannedcaractere.length); j++) {
      let test = contenuespacemenu[i] == bannedcaractere[j];
      if (test) {
        i = i+1;
      } else {
        caractereinside[i] = contenuespacemenu[i];
      }
    }
  }


console.log(caractereinside);

结果是:

共有2个答案

伍弘盛
2023-03-14
const contenuespacemenu = $('.Espace-Menu').children('div').html(); //the array which contains characters i want to delete.  
const bannedcaractere = ["\n", "\t"];
const caractereinside = contenuespacemenu.slice(0, contenuespacemenu.length); // I copy the table so as not to risk modifying it later

for (let i = 0; i < 50; i++) {
  let test = bannedcaractere.includes(caractereinside[i]);
  if (test) {
    caractereinside.splice(i, 1);
  } 
}

// 50 must be the length of the characterinside array if you want to iterate over the whole array

console.log(caractereinside);
const contenuespacemenu = $('.Espace-Menu').children('div').html(); //the array which contains characters i want to delete.
const bannedcaractere = ["\n", "\t"];
const caractereinside = [];

for (const i = 0; i < 50; i++) {
  caractereinside[i] = contenuespacemenu[i];
  let test = bannedcaractere.includes(caractereinside[i]);
  if (test) {
    caractereinside.splice(i, 1);
  }
}

// 50 must be the length of the characterinside array if you want to iterate over the whole contenuespacemenu

console.log(caractereinside);
齐元章
2023-03-14

不知道为什么使用2个循环,但我已经通过更改以下内容将您的最后一个示例更改为工作状态:

  • 将硬编码的50更改为contentuespacemenu.length
  • 我将使用,而不是关闭第二个循环/测试!BannedCharactere.includes(ContenueSpace菜单[i])检查当前字符是否有效
  • 删除i=i1 处理迭代器
  • 使用push()
var contenuespacemenu = ["1","2","3","4","5","6","7","8", "1","2","3"];
var bannedcaractere = ["1", "2","3","4"];
var caractereinside = [];

// Remove all from bannedCars
for (var i = 0; i < contenuespacemenu.length; i++) {
    if (!bannedcaractere.includes(contenuespacemenu[i])) {
      caractereinside.push(contenuespacemenu[i]);
    }
}

// Result
console.log(caractereinside);

 类似资料:
  • 我一直试图在班上解决这个问题,但我似乎无法正确计算。 用于。。循环,编写程序获取5名员工的姓名、工资和状态。计算每个员工的联邦税、州税和净工资。 由于某种原因,在两种不同的报表中,当就业工资的价值乘以8和9时,计算是不正确的,我不明白为什么。我希望我没说错x_x

  • 我有下面的一段代码,它正在打印:一些(600)作为输出。 不知道加法是如何在“for”循环中发生的。 在下面,它混淆了“for”循环的代码块内发生的事情以及变量“y”的计算方式。有人能帮忙吗?

  • 出于某种原因,当我尝试运行这段代码时,它总是返回找不到符号。然后我发现问题是变量无法解析为变量。我不知道为什么它无法将其解析为变量。

  • 我正在为Codewars做一个kata,它将两个数字数组相对放置。“对手”数组的平均数总是大于“codewarrior”数组,并且两个数组的长度总是相同的。我需要做的是找到获得胜利的最有效方法(codewarrior[x] 在上面,我将两个数组从最小到最大排序。然后,我有一个大的while循环来迭代“codefighter or”数组,以及两个time循环和一个if语句,每次迭代“对手”数组时,都

  • 在上面的代码中,我根据天气与否计算每个指数,每个指数都是=到零。然而,它总是在foreach循环中运行false,在for循环中运行true。有人能解释一下幕后发生了什么让这一切发生吗? 我不是按顺序运行它们,它们都是为了演示目的。

  • 我想为我的网格创建分页链接。我将maxPages(number)属性传递给组件,但我不能在render方法中使用。我能做什么?