Public
Edited
Aug 17, 2024
1 fork
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
Insert cell
observations = await fetchAllObservations()
Insert cell
observations[0]
Insert cell
uniques = {
// Use a Set to automatically remove duplicates
const uniqueNamesSet = new Set(
observations.map((obs) => obs.species_guess).filter(() => name != null) // Remove null or undefined names
);

// Convert the Set back to an array
return Array.from(uniqueNamesSet);
}
Insert cell
function getUniqueTaxaWithCounts(observations) {
// Create a Map to store unique taxa by id
const taxaMap = new Map();

observations.forEach((obs) => {
if (obs.taxon && obs.taxon.id) {
if (taxaMap.has(obs.taxon.id)) {
// If we've seen this taxon before, increment its count
const existingEntry = taxaMap.get(obs.taxon.id);
existingEntry.count++;

// Update the most recent observation if this one is more recent
if (obs.time_observed_at > existingEntry.recent.time_observed_at) {
existingEntry.recent = obs;
}
} else {
// If it's a new taxon, add it to the map with count 1 and set this as the most recent observation
taxaMap.set(obs.taxon.id, { taxon: obs.taxon, count: 1, recent: obs });
}
}
});

// Convert the Map values to an array
return Array.from(taxaMap.values());
}
Insert cell
taxaWithCounts = getUniqueTaxaWithCounts(observations).sort(
(a, b) => b.count - a.count
)
Insert cell

Purpose-built for displays of data

Observable is your go-to platform for exploring data and creating expressive data visualizations. Use reactive JavaScript notebooks for prototyping and a collaborative canvas for visual data exploration and dashboard creation.
Learn more