Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

140
Views
Set State to Array Filter value on condition

So i want to use array.filter to values that are not empty string in an object in the following senario

setData(
                _.filter(rows, item => {
                    return (
                        item.firstName.toLowerCase().includes(filterTable.searchByName.toLowerCase()) &&
                        item.email.toLowerCase().includes(filterTable.searchByEmail.toLowerCase()) &&
                        item.cognitoId.toLowerCase().includes(filterTable.searchByCognitoId.toLowerCase()) &&
                        item.mfaEnabled.toString() === filterTable.searchByMfa.toString()
                    );
                })
            );

i can use ifelse to what values are there but that will be a hell of a long ifelse

in the above code i want to add those items in _.filter that's value is !== ''

for example if filterTable.email === '' then i want following

setData(
                _.filter(rows, item => {
                    return (
                        item.firstName.toLowerCase().includes(filterTable.searchByName.toLowerCase()) && 
                        item.cognitoId.toLowerCase().includes(filterTable.searchByCognitoId.toLowerCase()) &&
                        item.mfaEnabled.toString() === filterTable.searchByMfa.toString()
                    );
                })
            );
about 4 years ago · Juan Pablo Isaza
2 answers
Answer question

0

Add another && for the search property

Try like this

setData(
    _.filter(rows, item => {
        return (
            filterTable?.searchByName &&
            item.firstName
                .toLowerCase()
                .includes(filterTable.searchByName.toLowerCase()) &&
            filterTable?.searchByEmail &&
            item.email
                .toLowerCase()
                .includes(filterTable.searchByEmail.toLowerCase()) &&
            filterTable?.searchByCognitoId &&
            item.cognitoId
                .toLowerCase()
                .includes(filterTable.searchByCognitoId.toLowerCase()) &&
            item.mfaEnabled.toString() === filterTable.searchByMfa.toString()
        );
    })
);
about 4 years ago · Juan Pablo Isaza Report

0

This fixed it thanks Amila for helping

setData(
            _.filter(rows, item => {
                return (
                    (filterTable?.searchByName !== ''
                        ? item.firstName.toLowerCase().includes(filterTable.searchByName.toLowerCase()) ||
                          item.lastName.toLowerCase().includes(filterTable.searchByName.toLowerCase())
                        : true) &&
                    (filterTable?.searchByEmail !== ''
                        ? item.email.toLowerCase().includes(filterTable.searchByEmail.toLowerCase())
                        : true) &&
                    (filterTable?.searchByCognitoId !== ''
                        ? item.cognitoId.toLowerCase().includes(filterTable.searchByCognitoId.toLowerCase())
                        : true)
                );
            })
        );
about 4 years ago · Juan Pablo Isaza Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!