不明白这个 "for" 循环的结果 (reddit)

D'ont understand the result of this "for" loop (reedit)

我想做一个小程序,从数组中删除一些不需要的字符,如“/n”、“/t”,问题是它部分起作用。我看到程序删除了我想要的一些字符,但不是所有索引都删除了(例如:第 3 和第 5):

程序:

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);

我明白了:

我也试过用一个 break 代替 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 contenuespacemenu 定义的数组,还更改了 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 个循环,但我通过更改以下内容将您的最后一个示例更改为工作状态:

  • 将硬编码 50 更改为 contenuespacemenu.length
  • 而不是关闭第二个循环/test,我将使用!bannedcaractere.includes(contenuespacemenu[i])检查当前字符是否有效
  • 删除了 i = i+1; 因为我们应该让 for 处理迭代器
  • 使用 push() 而不是关闭 caractereinside[i] 以防止 undefined 删除索引

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);


也就是说,上面的内容可以通过使用 filterincludes 来简化:

var contenuespacemenu = ["1","2","3","4","5","6","7","8", "1","2","3"];
var bannedcaractere = ["1", "2","3","4"];

// Filter
let caractereinside = contenuespacemenu.filter(n => !bannedcaractere.includes(n));

// Result
console.log(caractereinside);

如果 contenuespacemenu 是数组,请尝试使用 :

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);

否则,如果 contenuespacemenu 一堆文本,请尝试使用 :

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);