+ 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.
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
+ 1
Thanks a lot. I suppose that the 6 stands for the length of the string I use for filtering.
+ 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.
0
TheWhiteCat Even better, thank you.