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 filter data from array in object react

this is my data

"tester": [{ "title": "test1",
"id": "1",
"description": "abcdefg",
"photos": ["1.jpg","2.jpg"],
"tags": ["Sample","Help","OMG","Assignment","Bug"]
};

my solution right now but it can not search each letter in the tags 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 fliterdata = trips.filter((filter) => {
        if (searchTerm === '') {
            return filter;
        } else {
            return filter.title.toLowerCase() && //search word in title
                filter.description.toLowerCase() && //search word in description
                filter.tags.map((tag, index) => filter.tags[index].toLowerCase()).includes(searchTerm); //search each letter in the tags array
        }
    });

>Solution :

You forgot to actually compare the strings. You can do it elegantly by combining all values to match into a single array like this:

const searchTermLowercased = searchTerm.toLowerCase();
const filterdata = searchTerm === '' ? trips : trips.filter(
    ({ title, description, tags }) => [title, description, ...tags].some(
        value => value.toLowerCase().includes(searchTermLowercased)
    )
);
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