Javascript function to filter a an array of strings based on starting letters of the strings in the array

I need help writing a Javascript function which will take a string as an arugment and then filter out an array of strings starting with the string I pass as an argument.
So if I have the following array:
let words = ['simple', 'single', 'side', 'sacked', 'apple', 'orange']
I need a javascript funtion that will look at every string in this array, and give me a second array called myFilteredWords which start with the letters 'si" - assumuming that I pass 'si" as an argument to the funtion.
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Like this?
function f( s, a ) {
    var sl = s.length;
    var na = [];
    for( var i in a )
        if( a[i].substr( 0, sl ) == s )
    return na;

Open in new window

Julian HansenCommented:
You can use the Array.filter() function
This should do it
let haystack = ['apple','boomerang','bolivia','childern','delta','boland'];
let needle = 'bo';
let newData = haystack.filter(item => item.substr(0, needle.length) == needle);

Open in new window

For backward compatibility
var haystack = ['apple','boomerang','bolivia','childern','delta','boland'];
var needle = 'bo';
// In case arrow functions are not supported on target platform
var newData = haystack.filter(function(item) {
   return item.substr(0, needle.length) == needle;

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
NorieAnalyst Assistant Commented:
Can' t you use filter?
var words = ['simple', 'single', 'side', 'sacked', 'apple', 'orange'];

var begins = 'si';

var myFilteredWords =[];

myFilteredWords =  words.filter(word => word.startsWith(begins));



Open in new window

Introduction to Web Design

Develop a strong foundation and understanding of web design by learning HTML, CSS, and additional tools to help you develop your own website.

Eddie ShipmanAll-around developerCommented:
Give this a try:
var words = ['simple', 'single', 'side', 'sacked', 'apple', 'orange'];
const regex = new RegExp('^si', 'g')
const myFilteredWords  = words.filter(value => regex.test(value));

Open in new window

Julian HansenCommented:
startsWith() does not appear to be supported on IE
NorieAnalyst Assistant Commented:

I think you are right, it's not supported in IE11 anyway - not sure about other versions.
FaheemAhmadGulAuthor Commented:
Many thanks all the experts for your comments. As both Julian's and Norie's solutions were most helpful and I accepting both. Hope this is OK.
Julian HansenCommented:
You are welcome.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.