Dylan Brinkley
Dylan Brinkley

Reputation: 37

.filter to iterate over an array

I am trying to iterate over an array to filter out certain words that I do not want in a new array.

I can do it with a for loop, but i'm learning and trying to use iterators.

The code I have is below:

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. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

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

let storyWords = story.split(' ');

let betterWords = storyWords.filter(function(words) {
     return words !== unnecessaryWords[0];
});

console.log(betterWords.join(' '));

I am trying to take the unnecessaryWords out of the storyWords(story) array. As it is now it will take out the first element in the unnecessary words, but I can't get it to do all three. Thanks for the help!

Upvotes: 1

Views: 2951

Answers (3)

Yogen Darji
Yogen Darji

Reputation: 3300

var filtered = storyWords.filter(function(e) {
    return this.indexOf(e) < 0;
}, unnecessarywords);

Upvotes: 0

Abhinav Galodha
Abhinav Galodha

Reputation: 9878

You can make use of includes to check if the element exists in the array or not. Array includes method determines whether an array includes a certain element, returning true or false as appropriate.

Just note that it is a ES6 Feature. So, please check the browser compatibility or use a polyfill.

unnecessaryWords.includes(words);

Complete Code:

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. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

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

let storyWords = story.split(' ');

let betterWords = storyWords.filter(function(words) {
     return !unnecessaryWords.includes(words);
});

console.log(betterWords.join(' '));

Upvotes: 4

Dij
Dij

Reputation: 9808

you can use indexOf() here, check if the word is in unnecessaryWords array using indexOf and filter it out.

   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. I stopped at Riverbank State Park to take some extremely artsy photos. It was a short stop, though, because I had a really long way left to go. After a quick photo op at the very popular Little Red Lighthouse, I began my trek across the George Washington Bridge into New Jersey.  The GW is actually very long - 4,760 feet! I was already very tired by the time I got to the other side.  An hour later, I reached Greenbrook Nature Sanctuary, an extremely beautiful park along the coast of the Hudson.  Something that was very surprising to me was that near the end of the route you actually cross back into New York! At this point, you are very close to the end.';

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

let storyWords = story.split(' ');

let betterWords = storyWords.filter(function(words) {
     return unnecessaryWords.indexOf(words) < 0;
});

console.log(betterWords.join(' '));

Upvotes: 2

Related Questions