我有五篇关于这些初始属性的文章:
const articles = [
{ id: 1, views: 92, likes: 0, shares: 2, trendingValue: ? },
{ id: 2, views: 14, likes: 2, shares: 1, trendingValue: ? },
{ id: 3, views: 39, likes: 3, shares: 1, trendingValue: ? },
{ id: 4, views: 87, likes: 0, shares: 1, trendingValue: ? },
{ id: 5, views: 8, likes: 1, shares: 0, trendingValue: ? }
];
我还有一个全局统计对象,一旦文章获得新视图,喜欢或分享(或每周一次),就应该自动更新:
const stats = {
totalArticles: 5,
totalViews: 240,
totalLikes: 6,
totalShares: 5,
trendingCriteria: 0
};
到目前为止,我相信有一些公式可以用文章来完成。 trendingValue
和统计数据' trendingCriteria
。基本上是"趋势"需要具有与标准相等或更高的数字。这意味着,无论何时文章获得新视图,分享或喜欢,trendingValue
都必须根据全球统计数据中的观看,喜欢和分享的百分比进行更新。对应。
一个例子(它没有完全奏效)是:
此公式适用于文章创建其trendingValue
:
const article = articles[0]; // Article with id 1
article.views++; // Increment the views count
stats.totalViews++ // Increment the total views count
let percentSum = (
(article.views / stats.totalViews) + // = 0.3833
(article.likes / stats.totalLikes) + // = 0
(article.shares / stats.totalShares) // = 0.4
); // = 0.7833
// The trendingValue needs to be a higher value of trendingCriteria
// before refreshing trendingCriteria.
article.trendingValue = (stats.trendingCriteria +
(percentSum / stats.trendingCriteria)
);
trendingCriteria
。基本逻辑是;如果新的trendingCriteria
高于文章的trendingValue
,那么该文章就不再是"趋势"。第三步是我被困的地方。如何创建此值?是否可以为每个新视图更新此值,例如和共享?或者我是否必须每周更新一次值?
感谢所有回复。不幸的是,我无法使用它们,因为我对建议的解决方案有什么困惑。
无论如何,我尝试了另一个使用纪元时间戳和平均视图,喜欢和分享的解决方案。不确定它是否在实践中有效,所以如果有人能确认我会感激不尽。
function refreshArticleAtIndex(index, props) {
const data = articles[index];
// Increment props
if(props.view) { data.views++; stats.views++; }
else if(props.like) { data.likes++; stats.likes++; }
else if(props.share) { data.shares++; stats.shares++; }
// Refresh trendingRate
data.trendingRate = (() => {
const calcViews = data.views / stats.views;
const calcLikes = data.likes / stats.likes;
const calcShares = data.shares / stats.shares;
let value = Date.now() * (
(isFinite(calcViews) ? calcViews : 0) +
(isFinite(calcLikes) ? calcLikes : 0) +
(isFinite(calcShares) ? calcShares : 0)
);
return Math.round(value);
})();
}
function logArticles() {
const arr = articles.map(article => article);
arr.sort((a, b) => a.trendingRate > b.trendingRate ? -1 : 1);
arr.forEach(a => console.log(a.id +" |", a.trendingRate));
console.log("----------");
}
const stats = { views: 239, likes: 6, shares: 5 };
const articles = [
{ id: 1, views: 91, likes: 0, shares: 2, trendingRate: 0 },
{ id: 2, views: 14, likes: 2, shares: 1, trendingRate: 0 },
{ id: 3, views: 39, likes: 3, shares: 1, trendingRate: 0 },
{ id: 4, views: 87, likes: 0, shares: 1, trendingRate: 0 },
{ id: 5, views: 8, likes: 1, shares: 0, trendingRate: 0 }
];
console.log("ID | trendingRate");
// ================================================
// Add 1 view to article 1
refreshArticleAtIndex(0, { view: true });
// Add nothing to below articles, but refresh their trendingRate
refreshArticleAtIndex(1, {});
refreshArticleAtIndex(2, {});
refreshArticleAtIndex(3, {});
refreshArticleAtIndex(4, {});
logArticles();
// Add 1 like to article 1
refreshArticleAtIndex(0, { like: true });
logArticles();

第一个日志将根据每篇文章的视图,喜欢和分享的组合显示正确的顺序。接下来,第1条就像是一个类似的东西,它将其推向最热门的文章。
问题是这是否是一个有效的解决方案?
答案 0 :(得分:0)
趋势可能应该意味着“活动的前n%”或“按活动排名前n的文章”。
为此,你可以简单地将trendingValue保存到如下数组:{ article: articleId, trendingValue: trendingValue}
然后当你去寻找最热门的文章时,你会:
let cnt = trendingValues.length * myLimit
let i = 0
let trending = trendingValues.sort((a,b) => a.trendingValue > b.trendingValue).filter(() => i++ < cnt)
要使用您的语言,可以根据文章集将trendingCriteria设置为某个最小值。从那里可以进行微调以确保一定的最小活动(随着平均文章年龄开始增长)。