2017-04-09 38 views
0

我与这些初始属性五篇文章,以创建一个趋势公式:尝试基于某些条件

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有关于更新到它的次数百分比,喜股,由全球统计同行。

一个例子(其不完全工作)是:

  1. 用户视图物品1
  2. 此公式的文章运行来创建其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) 
    ); 
    
  3. 接下来,trendingCriteria应根据更新的文章进行更新。基本的逻辑是;如果新的trendingCriteria高于文章的trendingValue,则该文章不应该是“趋势”。

第三步是在那里我卡住了。我如何创建这个值?这个值是否可以更新为每个新的视图,如分享?或者我必须每周更新一次价值?

更新

感谢您的回复。不幸的是,我不能使用它们,因为我还不知道如何处理建议的解决方案。

无论如何,我试过另一种解决方案是利用一个时代时间戳以及平均观看次数,喜欢和股票。不知道它在实践中是否有效,所以如果有人能证实我会很感激。

function refreshArticleAtIndex(index, props) { 
 
    const data = articles[index]; 
 
\t 
 
    // 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 = (() => { 
 
    \t const calcViews = data.views/stats.views; 
 
    \t const calcLikes = data.likes/stats.likes; 
 
    \t const calcShares = data.shares/stats.shares; 
 
    \t let value = Date.now() * (
 
     (isFinite(calcViews) ? calcViews : 0) + 
 
     (isFinite(calcLikes) ? calcLikes : 0) + 
 
     (isFinite(calcShares) ? calcShares : 0) 
 
    ); 
 
    
 
    \t 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

你如何判断'trendingValue'为零? –

+0

等于0的'trendingValue'是具有零视图的新文章,例如和份额。但是,由于我要求提供此值的工作公式,因此我在此例中将其设置为0。所以在这种情况下这并不意味着什么。编辑:我现在将它们改为问号。 –

+0

percentSum应该是一个几何体而不是简单的总体或平均值:) –

回答

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可以根据一组文章设置为最小值。从那里可以很好地调整,以确保一定的最低活动(平均文章年龄开始增长)。

+0

感谢您的回复。不幸的是,我不明白该怎么做。你介意对我发布的更新进行检查吗? –