First, search the help docs and this forum. Maybe your question has been answered! The debugging steps can help, too. Still stuck? Delete this line and proceed.
What I’m trying to do
I’m doing a bookshelf and i’m looking for a way to display my books by series with all the series sorted by average rating calculated for each series.
Things I have tried
let groups = dv.pages('"Bibliothèque/Livres"')
.filter(b => b.series && b.shelves == 'read')
.map(b => { b.cleanedSeries = b.series.replace(/[^a-zA-Z\s]/g, '').trim().toUpperCase();
return b;
})
.groupBy(b => b.cleanedSeries)
.sort((a, b) => a.key.localeCompare(b.key));
for(let group of groups) {
let rows = group.rows.map(b => `<img src="${b.cover}" style="height: 160px; margin-right: 6px; margin-bottom: 6px; border-radius: 4px;" />`) .join("");
let totalRating = 0;
let totalRatingAvg = 0;
let ratedBooksCount = 0;
for (let book of group.rows) {
if (book.rating !== undefined && book.rating !== null) {
totalRating += parseFloat(book.rating);
totalRatingAvg += parseFloat(book.avgRating);
ratedBooksCount++;
}
}
group.average = ratedBooksCount > 0 ? totalRating / ratedBooksCount : 0;
group.average = Math.round(group.average * 100) / 100;
group.average1 = ratedBooksCount > 0 ? totalRatingAvg / ratedBooksCount : 0;
group.average1 = Math.round(group.average1 * 100) / 100;
let average = group.average1;
dv.header(3, `${group.key}, AvgRating:(${group.average1})`);
dv.el('div', { Livres: `<span></span> <details> <summary> </summary> <div>${rows}</div> </details> ` });
}
This display my books in each series but i dont succeed to sort the series based on the average rating that i,m calculating.
Thanks for your help!