0

I'm using indexOf to check a JSON twitter feed for certain strings. I want to have the ability to have multiple strings trigger the same event.

I wrote a switch that works if you're only looking for a single property in the array, but can't find a way to do this.

My first thought was trying

switch(true) {
case tweetContent.indexOf(Happy[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) !== -1: 
    // do a thing
break;  

Which you can see in context below. Really lost here.

$.getJSON("tweets_JSON.php?count=5").done(function(json) {

    console.log(json[0])

    var Happy = ["hashtag1", "#hashtag2", "#hashtag3"];

    var Sad = ["#hashtag4", "#hashtag5", "#hashtag6"];

    var AgencyLife = ["#hashtag7", "#hashtag8", "#hashtag9"];

    var tweetContent = json[0].text;

    // Searchs text property of first tweet in feed for triggers

    switch(true) {
    case tweetContent.indexOf(Happy[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15]) !== -1: 
        console.log("horray!");
    break; 
    case tweetContent.indexOf() !== -1: 
        console.log("boo!");
    break;
    case tweetContent.indexOf() !== -1:
        console.log("boo!");
    break;
    case tweetContent.indexOf() !== -1:
        console.log("boo!");
    break;
    case tweetContent.indexOf() !== -1:
        console.log("boo!");
    break;
    default:
        console.log("No Trigger Found. Light State unchanged.");
    break;        
    }        

});
Rob McCoy
  • 43
  • 1
  • 1
  • 4

2 Answers2

0

I'm not sure what exactly you are trying to do here, but I can give you an idea on

Is there a ways to check for multiple values on an array?

You can join the array to a long string and use RegExp to test the values:

var arr = ["hello", "good", "day", "twitter"];
var joindArr = arr.join(); // Or use delimiter arr.join('*’)
joindArr.match(/twitter|hello/g); // Return value will be array of matches 

If you only need to know if one of the values exist in the array check the length of matched array. In your case:

case joindArr.match(/twitter|hello/g).length > 0:
Erez Haim
  • 967
  • 8
  • 11
  • Sorry. I explained myself poorly. Essentially what I'm trying to do is `indexOf` a string pulled from JSON data, then see if it contains one or more values from a pre-defined array. There are multiple arrays, but every value in a single array would trigger the same event. I'm using a switch statement, but don't want to write and a case for each array value in every single array because well, that would be a TON of cases. basically it boils down to "Does this string contain ANY values from this other array?" if not does it contain any values from another array? Make more sense? – Rob McCoy Apr 29 '15 at 00:22
  • Yes, you can use what I offered above. convert the pre-defined array to RegExp. Now each array to a long string (you can event join the these strings to a very long string) and check for matches. If there is at least one match the answer is yes, else none of your pre-defined values are in any of the arrays – Erez Haim Apr 29 '15 at 01:08
0

You should be able to do this with $.grep.

var happy_matches = $.grep(Happy, function( hashtag, i ) {
  return tweetContent.indexOf("hashtag");
});
Sharn White
  • 624
  • 3
  • 15