trying to build a filter for the project I'm building. I want to parse through the JSON I'm getting from the backend API, filter out the job posts based on the "location" and display it.
useEffect(async () => {
let condition = await getJobs();
switch (jobCondition) {
case 'On-site': condition.filter((condition.jobLocation === 'in_person'));
break;
case 'Remote': condition.filter((condition.jobLocation === 'remote'));
break;
case 'Hybrid': condition.filter((condition.jobLocation === 'hybrid'));
break;
default:
condition = null;
}
if (condition != null) {
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: datePosted || null,
conditions: condition.data,
});
history.push({ pathname: '/', search: `${params.toString()}` });
return;
}
const params = new URLSearchParams({
jobs: jobFilter || null,
location: locationFilter || null,
since: null,
conditions: null,
});
history.push({ pathname: '/', search: `${params.toString()}` });
}, [jobCondition]);
I am clearly doing something wrong here because it does not even change my URL params.
CodePudding user response:
You need to pass a callback function with one argument as the first parameter and then return a boolean:
condition.filter((con) => con.jobLocation === 'in_person');
By the way, if you want to get the filtered array, you need to reassign condition
.