1

Im playing around with React and json data, i have an input field to search data from the json file.

I can figure out how to search one key of the json data, is there any chance of some help.

My code is:

if(searchString.length > 0){
    contactsData = contactsData.filter(function(l){
        return l.name.toLowerCase().match( searchString );
    });
}

where l.name i also want l.company and l.email.

0

2 Answers 2

2

One simple solution is concatenate all the three strings by using some character like *, # or any other, then check for searchString.

Like this:

if(searchString.length > 0){
    let str;
    contactsData = contactsData.filter(function(l){
        str = `${l.name}# ${l.company}# ${l.email}`;
        return str.toLowerCase().match( searchString );
    });
}

Or you can individually check also, like this:

return (
    l.name.toLowerCase().match( searchString ) || 
    l.company.toLowerCase().match( searchString ) || 
    l.email.toLowerCase().match( searchString )
);
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect, not sure why i didn't think of going down the option 2 route, for now i will go with that option as for me its a bit more readable.
0

search on multiple condition in the given array you can search against firstName,lastName,email,phone. if search query matched any of this values it will result of that.

{[{firstName:'Ali',lastName:'khan',email:'[email protected]',phone:12345}] .filter((a) => ${a.firstName} ${a.lastName} ${a.email} ${a.phone}.includes( your search filed value here ) )

1 Comment

As it’s currently written, your answer is unclear. Please edit to add additional details that will help others understand how this addresses the question asked. You can find more information on how to write good answers in the help center.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.