Fillter in an array
JavaScript
x
6
1
const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];
2
3
const result = words.filter(word => word.includes('e'));
4
5
console.log(result);
6
// expected output: Array ["elite", "exuberant", "destruction", "present"]
But if an array consists of many arrays inside, I am not finding a reasonable way to solve it.
Example: with this array
JavaScript
1
7
1
const array = [
2
["aaaa", "b", "c", "d", "e", "f", "g"],
3
["h", "i", "j", "k", "l", "m", "n"],
4
["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
5
["o", "o", "o", "o", "o", "o"],
6
];
7
How to fillter ‘a’ and get result
JavaScript
1
5
1
result = [
2
["aaaa", "b", "c", "d", "e", "f", "g"],
3
["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
4
];
5
Advertisement
Answer
Use filter
and some
JavaScript
1
10
10
1
const data = [
2
["aaaa", "b", "c", "d", "e", "f", "g"],
3
["h", "i", "j", "k", "l", "m", "n"],
4
["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
5
["o", "o", "o", "o", "o", "o"],
6
];
7
8
const output = data.filter((arr) => arr.some((item) => item.includes("a")));
9
10
console.log(output);