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

Filtering an object where i need to check multiple properties to check if they contain one of the strings in an array

So I have an array with strings = ["ethan" , "kathrine"]

And i have an object with multiple properties, but im only interested in the properties name, and description

For example

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

arr = [
{name: "ethaniel" , description: "strong" , age: 200} ,
{name: "kathrine" , description: "beautiful" , age: 220},
{name: "zack" , description: "he loves kathrine" , age: 133},
{name: "ethan" , description: "he loves kathrine" , age: 133},
{name: "bob" , description: "he loves trucks" , age: 133}
]

The resulting array should be

arr = [
{name: "ethaniel" , description: "strong" , age: 200} ,
{name: "kathrine" , description: "beautiful" , age: 220},
{name: "zack" , description: "he loves kathrine" , age: 133},
{name: "ethan" , description: "he loves kathrine" , age: 133}
]

Because each one of them contains ethan or kathrine in the name or the description

My idea is do this

filteredArry = arr.filter(i => {return i.name.some('a') || i.description.includes('a')})

Or somethign like that

I just dont understand , if i have to use some or includes
and how can i include all the content of the array that has the keywords in the include/some method

>Solution :

Array#some:

tests whether at least one element in the array passes the test implemented by the provided function. It returns true if, in the array, it finds an element for which the provided function returns true; otherwise it returns false. It doesn’t modify the array.

Sring#includes:

performs a case-sensitive search to determine whether one string may be found within another string, returning true or false as appropriate.

Therefore, in the filter iterations, you need to check if any of the sub-strings is either in the name or description:

const 
  arr = [ {name: "ethaniel" , description: "strong" , age: 200} , {name: "kathrine" , description: "beautiful" , age: 220}, {name: "zack" , description: "he loves kathrine" , age: 133}, {name: "ethan" , description: "he loves kathrine" , age: 133}, {name: "bob" , description: "he loves trucks" , age: 133} ],
  subStrs = ["ethan" , "kathrine"];

const filteredArry = arr.filter(({ name, description }) => 
  subStrs.some(subStr => name.includes(subStr) || description.includes(subStr))
);

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