Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Search Array of strings for matching character sequence

I have an array like this

const arr = ["Apple", "Banana", "Cherry", "Orange", "Avocado", "Ananas", "Pineapple];

I need a function that returns an array with all the Elements from arr, that match the given character sequence. The order of the characters is of importance.

Given "ppl" the function should return ["Apple", "Pineapple"].

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Given "n" the function should return ["Banana", "Orange", "Ananas", "Pineapple"].

Given "voca" the function shoould return ["Avocado"]

Given "rr" the function should return ["Cherry"], while given a "r" the function should return ["Cherry", "Orange"].

>Solution :

try this

const arr = ["Apple", "Banana", "Cherry", "Orange", "Avocado", "Ananas", "Pineapple"];


const search = query => arr.filter(s => s.toLowerCase().includes(query))

console.log(search("n"))
console.log(search("ppl"))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading