I have one array of strings. I need to find all strings starting with a key. for eg: if there is an array [\'apple\',\'ape\',\'open\',\'soap\'] when searched with
[\'apple\',\'ape\',\'open\',\'soap\']
You can use filter with regex to accomplish this:
function startsWith(array, key) { const matcher = new RegExp(`^${key}`, 'g'); return array.filter(word => word.match(matcher)); } const words = ['apple','ape','open','soap'] const key = 'ap' const result = startsWith(words, key)