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.
LVL 1
FaheemAhmadGulAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
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.

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

Open in new window

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

Open in new window

For backward compatibility
<script>
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;
});
console.log(newData);
</script>

Open in new window

0

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));

console.log(myFilteredWords);

console.log(myFilteredWords);

Open in new window

0
Big Business Goals? Which KPIs Will Help You

The most successful MSPs rely on metrics – known as key performance indicators (KPIs) – for making informed decisions that help their businesses thrive, rather than just survive. This eBook provides an overview of the most important KPIs used by top MSPs.

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));
console.log(myFilteredWords);

Open in new window

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

I think you are right, it's not supported in IE11 anyway - not sure about other versions.
0
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.
0
Julian HansenCommented:
You are welcome.
0
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
JavaScript

From novice to tech pro — start learning today.