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()
);
})
);
CodePudding user response:
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()
);
})
);
CodePudding user response:
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)
);
})
);