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);
T Shoats
  • 337
  • 2
  • 4
  • 18
  • 1
    I'm not sure how that code outputs "Trap Samples", because [`some`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/some) returns a Boolean... Also, [don't use `document.write`](http://stackoverflow.com/q/802854/215552) for, well, anything... – Heretic Monkey Apr 03 '17 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. – Josh Apr 03 '17 at 19:21

3 Answers3

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.

Bergi
  • 513,640
  • 108
  • 821
  • 1,164
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);
kind user
  • 32,209
  • 6
  • 49
  • 63
  • good solution, but I would rather use the filter function because of its simplicity. Thanks – T Shoats Apr 03 '17 at 18:34
  • @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. – kind user Apr 03 '17 at 18:35
  • Nope, using `filter` is the straightforward way to solve this problem. – Josh Apr 03 '17 at 18:37
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.

Josh
  • 559
  • 1
  • 3
  • 13