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"].
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"].
It is also possible like this
const arr = ["Apple", "Banana", "Cherry", "Orange", "Avocado", "Ananas", "Pineapple"];
const query = 'pp';
arr.filter(element => element.includes(query));
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"))
It's pretty straight forward using filter function and indexOf
const arr = ["Apple", "Banana", "Cherry", "Orange", "Avocado", "Ananas", "Pineapple"];
const cond = 'ppl'
console.log(arr.filter(x => x.indexOf(cond) > -1 ))