Empresas
Empregos
  • Sobre nós
  • Soluções
    • Publicação de vagas
      Publique sua vaga e receba candidatos qualificados em 48h.
    • Avaliações de candidatos
      Mais de 500 testes técnicos e psicológicos, mais anti-fraude.
    • Headhunting
      Busca executiva personalizada do início ao fim.
    • Folha de Pagamento + EOR
      Dispersão de folha e EOR em mais de 15 países da LATAM.
  • Preços
  • Empregos

0

91
Visualizações
Filtering an array with a loop not working

I don't see what the problem is. The same type of code worked fine for something similar before (jsfiddle https://jsfiddle.net/Montinyek/ufkdgz4t/3/), but now it just gives a strange error. Can someone please explain why this method worked for the jsfiddle example but not here?

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day.

let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = []
const storyWords = story.split(' ')

for(let i = 0; i < storyWords.length; i++) {
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] !== unnecessaryWords[j]) {
      betterWords.push(storyWords)
    }
  }
}

I also tried using the splice() method, again with weird results:

for(let i = 0; i < storyWords.length; i++) {
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] === unnecessaryWords[j]) {
      betterWords.push(storyWords.splice(unnecessaryWords[j], 1))
    }
  }
} 
about 4 years ago · Juan Pablo Isaza
2 Respostas
Responde à pergunta

0

As you mentioned there are other ways to achieve this. Here is one of them.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day.';

let unnecessaryWords = ['extremely', 'literally', 'actually' ];

const storyWords = story.split(' ')

const betterWords = storyWords.filter(sw => !unnecessaryWords.includes(sw));

console.log(betterWords);

about 4 years ago · Juan Pablo Isaza Relatório

0

You can create a variable which keeps track of whether the particular storyWord was found in unnecessaryWords, and only push the storyWord to betterWords if it's false.

let story = 'Last weekend, I took literally the most beautiful bike ride of my life. The route is called "The 9W to Nyack" and it actually stretches all the way from Riverside Park in Manhattan to South Nyack, New Jersey. It\'s really an adventure from beginning to end! It is a 48 mile loop and it basically took me an entire day.';

let unnecessaryWords = ['extremely', 'literally', 'actually' ];
const betterWords = []
const storyWords = story.split(' ')

for(let i = 0; i < storyWords.length; i++) {
  let foundInUnnecessary = false;
  for(let j = 0; j < unnecessaryWords.length; j++) {
    if (storyWords[i] === unnecessaryWords[j]) {
      foundInUnnecessary = true;
      break;
    }
  }
  if (!foundInUnnecessary) {
    betterWords.push(storyWords[i]);
  }
}

console.log(betterWords);

about 4 years ago · Juan Pablo Isaza Relatório
Responde à pergunta
Encontrar trabalhos remotos

Descubra a nova forma de encontrar um emprego!

melhores empregos
Principais categorias de trabalho
Empresas
Postar vaga Preços Comercial
Jurídico
Termos e Condições Política de privacidade
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomende algumas ofertas para mim
Preciso de ajuda