No veo cuál es el problema. El mismo tipo de código funcionó bien para algo similar antes (jsfiddle https://jsfiddle.net/Montinyek/ufkdgz4t/3/ ), pero ahora solo da un error extraño. ¿Puede alguien explicar por qué este método funcionó para el ejemplo jsfiddle pero no aquí?
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) } } }También intenté usar el método splice(), nuevamente con resultados extraños:
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)) } } }Como mencionaste, hay otras formas de lograr esto. Aqui esta uno de ellos.
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);Puede crear una variable que realice un seguimiento de si la palabra de la historia en particular se encontró en palabras innecesarias y solo enviar la palabra de la historia a mejores palabras si es falsa.
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);