0

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()
                    );
                })
            );

2 Answers 2

1

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()
        );
    })
);
Sign up to request clarification or add additional context in comments.

2 Comments

@Ahsan Baloch, check this out !!
Amila Senadheera no it does not work, ive already tried that. copied ur code too didnt work
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)
                );
            })
        );

Comments

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.