-3

i wanted to do a little program that delete some unwanted characters like "/n","/t" from an array,the issue is that it works in part. i saw that the program deletes some of the characters i want, but not for all indices (for example : the 3th et the 5th):

the program :

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

And it shows this result :

enter image description here

some of the unwanted characters are deleted but not all of them ... indices 3 and 5.

And here is the primary code which put the string content of an html tag into an array " caractereinside " character by character :

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

The result :

enter image description here

if i transform "var contenuespacemenu" into a regular array like so, it works :

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 get this :

enter image description here

i also tried to replace i = i+1 by a break like what i was talled, like so :

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

But it gave me this :

enter image description here

Finally, i tried again with i = i+1 with a defined array for var contenuespacemenu, also changed var bannedcharacter, and it worked like it supposed to be:

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

The result :

enter image description here

Fefe
  • 1
  • 4
  • 1
    Sound like you're overcomplicating thinks, why not something like [`includes()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/includes?retiredLocale=nl)? – 0stone0 Jun 07 '21 at 12:34
  • 2
    First of all, your `contenuespacemenu` is _not_ an array, jQuery’s `.html()` returns a _string_. Second, why is `i` running from 0 to 50, why is that not taking the actual length of `contenuespacemenu` into account in the first place? – CBroe Jun 07 '21 at 12:34
  • This could be done as a one-liner with a little regular expression … – CBroe Jun 07 '21 at 12:36
  • @0stone0, i'm going to try this, but i want to know why it doesn't work the way i wrote – Fefe Jun 07 '21 at 12:37
  • @CBroe Ok but what expression ? – Fefe Jun 07 '21 at 12:38
  • 4
    You're breaking your loop counter by incrementing `i` manually. – James Jun 07 '21 at 12:38
  • @CBroe yes this is a string. 0 to 50 because this was for the example, string would be too long .. – Fefe Jun 07 '21 at 12:41
  • Something like this? https://jsfiddle.net/jpLck1td/ – ChrisG Jun 07 '21 at 12:42
  • @ChrisG yes approximately, but it's a little bit complicated for me to understand your code, i'm a beginner in jquery – Fefe Jun 07 '21 at 12:50
  • 1
    There's no Jquery in you question: [What is the difference between JavaScript and jQuery?](https://stackoverflow.com/questions/20283098/what-is-the-difference-between-javascript-and-jquery). – 0stone0 Jun 07 '21 at 12:52
  • @0stone0 i think so, but i still don't understand why there is a problem with my for loop iteration – Fefe Jun 07 '21 at 12:55
  • I'm iterating over the divs using `.each()`, then I grab the text, filter it and write it back to the div. – ChrisG Jun 07 '21 at 12:57
  • @CBroe *not an array, a string* - a string *is* just an array or characters, ending in `\0` :) – freedomn-m Jun 07 '21 at 13:08
  • Usuming `contenuespacemenu` is a string, like [CBroe commented earlier](https://stackoverflow.com/questions/67871660/dont-understand-the-result-of-this-for-loop-reedit/67871923?noredirect=1#comment119965323_67871660). You can simply use `contenuespacemenu.replace(/[\n\t]+/g, "")`. Which replaces `\n` and `\t` characters with nothing (aka removes them). You can see the regex explanation [here](https://regexper.com/#%5B%5Cn%5Ct%5D%2B). – 3limin4t0r Jun 07 '21 at 16:59
  • @3limin4t0r i'll look for replace method – Fefe Jun 08 '21 at 19:04

2 Answers2

4

Not sure why you're using 2 loops, but I've changed your last example to a working state by changing the following:

  • Change hardcoded 50 to contenuespacemenu.length
  • Instead off the second loop / test, I'll use !bannedcaractere.includes(contenuespacemenu[i]) to check if current char is valid
  • Removed the i = i+1; since we should let the for handle the iterator
  • Using push() instead off caractereinside[i] to prevent undefined on removed indexes

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

That said, the above could be simplified by using the filter and includes:

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);
0stone0
  • 21,605
  • 3
  • 29
  • 49
0

If contenuespacemenu is an array try with :

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

Else If contenuespacemenu bunch of text try with :

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);
snd
  • 138
  • 6
  • i tried your modification but it doesn't work, showing me this message : Uncaught TypeError: caractereinside.splice is not a function – Fefe Jun 07 '21 at 13:05
  • can you print the caractereinside value before the for loop please. Because `Uncaught TypeError: caractereinside.splice` means that not define or it is not an array – snd Jun 07 '21 at 13:11
  • Or the error can from to the for loop you must have `for (let i = 0; i < 50; i++)` and not `for (const i = 0; i < 50; i++)`. i have tested it work fine for me with my own array data. Check the value of `caractereinside` before the loop if it does't work for you. It must work within the standards if the data is correct – snd Jun 07 '21 at 13:20
  • contenuespacemenu is not an array but a bunch of text – Fefe Jun 07 '21 at 13:35
  • OK I see. Please can you give me a small example of the content format contenuespacemenu? – snd Jun 07 '21 at 13:42
  • I updated the answer. `If contenuespacemenu is array` you test the first `Else you test the second`. Hope that will help you. Thank – snd Jun 07 '21 at 13:54
  • it worked ! thanks – Fefe Jun 07 '21 at 13:56
  • it worked you just have to change const i by var or let in for loop and const caractereinside by var or let caractereinside ! – Fefe Jun 07 '21 at 13:57
  • 1
    This answer suffers from a few problems. 1) The first code block iterates through `caractereinside`, and uses `splice` to remove unwanted elements. The issue is that `splice` will shift the array. `i` should be adjusted to accommodate for this change. Meaning that you should decrement `i` after `splice`ing. Otherwise the element shifting to the position of the removed element is never checked. 2) The second code block leaves the `caractereinside` array with empty slots, which may not be desired. – 3limin4t0r Jun 07 '21 at 16:41
  • @3limin4t0r, splice worked, i got the result that i want in my program in deleting all the unwanted characters from an array. I still don't know why the first program i wrote in the question shows me these kinds of results like i said, but there was also the fact that the first variable of the program wasn't returning an array but some text. and i missed to precise while i knew it, and had already done the switch putting text to an array. Anyway, splice method works well here. – Fefe Jun 08 '21 at 19:20
  • @Fefe Like the [comment under the question](https://stackoverflow.com/questions/67871660/dont-understand-the-result-of-this-for-loop-reedit/67872114?noredirect=1#comment119965441_67871660) and the [answer of 0stone0](https://stackoverflow.com/a/67871923/3982562) both said `i = i + 1` is why your initial program does not work. If you remove that line it should work (although you are still left with an array with empty slots). – 3limin4t0r Jun 08 '21 at 19:39
  • @3limin4t0r Yes this was for this reason, i understand now :) – Fefe Jun 08 '21 at 20:41