2017-04-09 37 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

感謝您的回覆。不幸的是,我不明白該怎麼做。你介意對我發佈的更新進行檢查嗎? –