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 can I search the whole object in array?

I have a table and a searchbox. I want to search all objects in array. When I type John, I want two objects to return because they are in the first and last name parts, and they are in both fields.

For example:

myArray = [
{id:1, name:'John', surname:'Vardy'},
{id:2, name:'Pepe', surname:'Vardy'},
{id:3, name:'Phil', surname:'John'},

]

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

>Solution :

You can combine filter with a search in Object.values:

found = myArray.filter(obj =>
   Object.values(obj).includes(searchString))
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