javascript - How do I check an array for patterns? -


how return array of indexes in contents match pattern.

var contents = ["dog big","cat small","horse huge"]  (contents,"og") //returns [0,2] (contents,"at") //returns [1] (contents,"..") //etc 

so, "o" , "g" both in 0 , 2 not in 1.

note: letters don't have in order.

you can use array.prototype.reduce , array.prototype.every filter out indices matches criteria - see demo below:

var contents = ["dog big","cat small","horse huge"];    function filter(filter) {    var f = filter.split('');    return contents.reduce(function(p,c,i){      f.every(e=>~c.indexof(e)) && p.push(i);      return p;    },[]);  }    console.log(filter("og"));  console.log(filter("at"));


Comments

Popular posts from this blog

asynchronous - C# WinSCP .NET assembly: How to upload multiple files asynchronously -

aws api gateway - SerializationException in posting new Records via Dynamodb Proxy Service in API -

asp.net - Problems sending emails from forum -