filter
needs the callback function to return a truthy or falsy value.
Your callback function returns a promise that resolves to true
or false
which will always be a truthy value.
Map the array to get the data you need, wait for the promises to resolve, then deal with the new values.
const baseArray = [/*...*/]
const arrayOfPromises = baseArray.map(
async (value) => ({
...value,
checkZone: await getOneParkingZone({id: zone.id})
}
);
const arrayWithCheckZone = await Promise.all(arrayOfPromises);
const filteredArray = arrayWithCheckZone.filter(value => value.checkZone);
const filteredArrayWithoutCheckZone =
filteredArray.map(value => {
const { checkZone, ...rest } = value;
return rest;
});
);
Or without the extra variables:
(await Promise.all([/*...*/].map(
async (value) => ({
...value,
checkZone: await getOneParkingZone({id: zone.id})
}
))).filter(value => value.checkZone).map(value => {
const { checkZone, ...rest } = value;
return rest;
});
);