I have a flatMap function that fetch and return data for each item in array. It also returns an empty array if item doesn't have certain informations. Since I'm using flatMap I was expecting that it will remove all the empty arrays, but the finished array still have them included.
Function:
Promise.all(
array.flatMap(async (item) => {
const fetchedData = await fetch(`${item.url}`)
.then(response => response.json())
.then(data => data.stats.length ? data : null);
return fetchedData ? { ...fetchedData } : [];
})).then(data => console.log(data));
Also when I log the data using console.log(data.flat()) instead of console.log(data) all the empty arrays are removed, but it would require iterating through array one more time. Any ideas why flatMap isn't doing it by itself?