I have an array of users and also an array of filters.
I need to filter users based on the array of filters but when I do it will filter on one array but not the others
My filters object looks like this:
filters: {
levels: [],
locations: ['Laois'],
subjects: ['Art']
}
My users look like this:
const users = [
{
email: '[email protected]',
levels: ['Ordinary'],
location: 'Laois',
subjects: ['Art', 'German']
},
{
email: '[email protected]',
levels: ['Higher', 'Ordinary'],
location: 'Laois',
subjects: ['English', 'German']
}
]
Based on the filter above, once filtered the users should only be one because it contains 'Art' and 'Laois':
[{
email: '[email protected]',
levels: ['Ordinary'],
location: 'Laois',
subjects: ['Art', 'German']
},
]
But I am still getting the two users:
EDIT My Code:
applyFilter = (visable) => {
let { filters, users } = this.state;
const { subjects, locations, levels } = filters;
let filteredUsers = [];
const filteredData = users.filter((user) =>
user.subjects.some(subject => subjects.includes(subject)) ||
locations.some(location => location === user.location) ||
user.levels.some(level => levels.includes(level))
);
console.log(filteredData)
if (!subjects.length && !locations.length && !levels.length) {
filteredUsers = users;
} else {
filteredUsers = filteredData;
}
this.setState({
filterModalVisible: visable,
filteredResults: filteredUsers.length >= 0 ? filteredUsers : [],
});
}