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


i'm using indexof check json twitter feed strings. want have ability have multiple strings trigger same event.

i wrote switch works if you're looking single property in array, can't find way this.

my first thought trying

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

which can see in context below. 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 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;             }          }); 

i'm not sure trying here, can give idea on

is there ways check multiple values on array?

you can join array long string , use regexp test values:

var arr = ["hello", "good", "day", "twitter"]; var joindarr = arr.join(); // or use delimiter arr.join('*’) joindarr.match(/twitter|hello/g); // return value array of matches  

if need know if 1 of values exist in array check length of matched array. in case:

case joindarr.match(/twitter|hello/g).length > 0: 

Comments

Popular posts from this blog

java - Spring Data JPA: Why findOne(id) executing delete query internally? -

python - Mongodb How to add addtional information when aggregating? -

java - Incorrect order of records in M-M relationship in hibernate -