0

how to get multiple occurences of words in a string. I've tried multiple functions but with no success. I know I can get back the first true value using some() method as below.

       var keyword_array = ["Trap","Samples","WAV","MIDI","Loops"];
        function validateContentKeywords(content,keyword){
                keyword.some(function(currentValue,index){

                     console.log(currentValue + " ");

                     return content.indexOf(currentValue) >= 0;         
                });          
        }
     // Outputs --> Trap Samples
       if(validateContentKeywords("Beat Loops WAV Trap Samples Dog Cat MIDI",keyword_array)){
                console.log("Matches");     
        }
    // What I Want is --> Trap,Samples,MIDI,Loops

The above function only outputs 2 occurences and I want it to output all of the matching values at the same time such as --> Trap,Samples,MIDI,Loops. Is there a way to get multiple occurences of words in a string at the same time?

UPDATED:: The solution that helped me out is below

           function Matches(value){
                return "Beat Loops WAV Trap Samples Dog Cat MIDI".indexOf(value) !== -1;
           }
           var keyword_array = ["Trap","Samples","WAV","MIDI","Loops"].filter(Matches);
            document.write(keyword_array);
2
  • 1
    I'm not sure how that code outputs "Trap Samples", because some returns a Boolean... Also, don't use document.write for, well, anything... Commented Apr 3, 2017 at 18:17
  • Your question has the es6 tag, so I guess you could just use the string's include method instead of defining your own. Please, check out my answer to this question. It is really as easy as it gets. Commented Apr 3, 2017 at 19:21

3 Answers 3

2

You seem to be looking for the filter Array method that returns an array of the matched elements, instead of an boolean value whether some matched.

Sign up to request clarification or add additional context in comments.

1 Comment

@ Bergi, Just what I wanted! Thanks
2

var keyword_array = ["Trap", "Samples", "WAV", "MIDI", "Loops"];

function validateContentKeywords(content, keyword) {
  var words = content.split(' '); //split the given string
  for (var i = 0; i < words.length; i++) {
    if (keyword.indexOf(words[i]) > -1) { //check if actually iterated word from string is in the provided keyword array
      document.write(words[i] + " "); //if it is, write it to the document
    };
  }
}

validateContentKeywords("Beat Loops WAV Trap Samples Dog Cat MIDI", keyword_array);

3 Comments

good solution, but I would rather use the filter function because of its simplicity. Thanks
@TitusShoats Actually this is the easiest solution possible. And about Bergi's answer, how does this information helps you out with your problem? Drop your working filter solution into your question.
Nope, using filter is the straightforward way to solve this problem.
0

The easiest way to do it would be this:

keyword_array.filter(keyword => searchString.includes(keyword));

You can learn more about filter here. I highly recommend learning about how to use map, reduce and filter. They are your best friends.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.