+ 1

How to filter elements in JavaScript array

I have an array like this [ "ad", "advert", "advertisement", "advent", "adventure", "adventurer", ... ] What I need is to return the following array, if I get text "adver" from an input field: [ "advert", "advertisement" ] Is there a way I can filter elements in an array like this? When I use SQL, I just do something like SELECT ... FROM ... WHERE text LIKE "adver%" and I need to do the exact same thing with JavaScript array.

23rd Dec 2020, 2:40 PM
Jan Štěch
Jan Štěch - avatar
5 ответов
+ 4
Jan Štěch , if you want to make it dynamic => to change the filter whenever you want, you can try this way. https://code.sololearn.com/W2Lx2jNLw602/?ref=app
23rd Dec 2020, 2:58 PM
TheWh¡teCat 🇧🇬
TheWh¡teCat 🇧🇬 - avatar
+ 1
Thanks a lot. I suppose that the 6 stands for the length of the string I use for filtering.
23rd Dec 2020, 2:55 PM
Jan Štěch
Jan Štěch - avatar
+ 1
Replace startsWith to includes and it should work for any substring within that string unlike startsWith which will only check at the beginning of the string. Just in case you need it.
23rd Dec 2020, 3:21 PM
Avinesh
Avinesh - avatar
0
TheWhiteCat Even better, thank you.
23rd Dec 2020, 3:06 PM
Jan Štěch
Jan Štěch - avatar