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

How to filter array with children are array?

Fillter in an array

const words = ['spray', 'limit', 'elite', 'exuberant', 'destruction', 'present'];

const result = words.filter(word => word.includes('e'));

console.log(result);
// 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

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

const array = [
  ["aaaa", "b", "c", "d", "e", "f", "g"],
  ["h", "i", "j", "k", "l", "m", "n"],
  ["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
  ["o", "o", "o", "o", "o", "o"],
];

How to fillter ‘a’ and get result

result = [
  ["aaaa", "b", "c", "d", "e", "f", "g"],
  ["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
];

>Solution :

Use filter and some

const data = [
    ["aaaa", "b", "c", "d", "e", "f", "g"],
    ["h", "i", "j", "k", "l", "m", "n"],
    ["haaaa", "i", "j", "k", "laaaa", "m", "naaaa"],
    ["o", "o", "o", "o", "o", "o"],
];

const output = data.filter((arr) => arr.some((item) => item.includes("a")));

console.log(output);
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