0

I'm trying to clean an array (secondArray) by removing those elements that are also included in another array (secondRegex). This is one of my latest attempts, but it doesn't work :'( The result I get is: 0[]. Why????????

const secondRegex = ['i', 'a', 'about', 'am', 'an', 'are', 'as', 'at', 'be', 'by', 'for', 'from', 'how', 'in', 'is', 'it', 'of', 'on', 'or', 'that', 'the', 'this', 'to', 'was', 'what', 'when', 'where', 'who', 'will', 'with']; const secondArray = ['i', 'am','a', 'very', 'happy', 'child']; let myList = [] for (let i = 0; i < secondArray; i++){ let is_the_same = 0; for (let j = 0; j < secondRegex; j++){ if(secondArray[i] == secondRegex[j]){ is_the_same = 1; break; } }if (!is_the_same){ myList.push(secondArray[i]); } } console.log(myList); 

I have also tried this with no success:

for (let i = 0; i < secondArray.length; i++){ let value = secondArray[i]; let coincidence = secondArray.some(value => secondRegex.includes(value)); }if (coincidence = true){ continue; } else{ myList.push(secondArray[i]); } 
3
  • 2
    Take a look at filter it can help you with your issue. Commented Nov 18, 2021 at 14:28
  • for (let i = 0; i < secondArray; i++){ You want for (let i = 0; i < secondArray.length; i++){ Same with inner loop. Commented Nov 18, 2021 at 14:32
  • Thank youuuuuuuuuuu!!!!!! Commented Nov 18, 2021 at 14:40

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.