2016-12-14 50 views
1

如何使用javascript計算摘要?

var data = [ 
 
    { 
 
     _id:   '6d2847090a6a1b', 
 
     user_id:  '580da5653bd3cc5802f6c37c', 
 
     name:   'aaaa', 
 
     sum: 250 
 
     
 
    }, 
 
    
 
    { 
 
     _id:   '75f36d2847090a6a36580871', 
 
     user_id:  '3bd3cc5802f6c37c580da565', 
 
     name:   'bbbb', 
 
     sum :30 
 
    }, 
 
    { 
 
     _id:   '6a3087184367090a6a3e58fd284', 
 
     user_id:  '2f63bc580c37c580da565d3c', 
 
     name:   'ccc', 
 
     sum :100 
 
    } 
 
]; 
 

 

 
var giftedObject = []; 
 
data.forEach(function(userSummary){ 
 
    userSummary.numberOfGifted = Math.floor(userSummary.sum/100); 
 
    for(var i = 0; i < userSummary.numberOfGifted; i++){ 
 
     giftedObject.push({ 
 
      user_id: userSummary.user_id, 
 
      name: userSummary.name, 
 
      gifted_point: 1, 
 
      active: true 
 
     }); 
 
    } 
 
    
 
}); 
 
var result = {giftedObject: giftedObject}; 
 
console.log(result);

以上是當過用戶達到100分giftedObjectobject將創造我的代碼。在這裏。

如果用戶達到100點時一個對象將創建

如果用戶達到了200點創建對象後兩個時間對象將創建

我們需要從數據降低了優值

預期的結果:

優對象的結果:

{ 
     active:true 
     user_id:  '580da5653bd3cc5802f6c37c', 
     gifted_point: 1 
     name:   'aaaa', 


    }, 

    { 
     active:true 
     user_id:  '580da5653bd3cc5802f6c37c', 
     gifted_point: 1 
     name:   'aaaa', 
    }, 
    { 

     active:true 
     user_id:  '2f63bc580c37c580da565d3c', 
     gifted_point: 1 
     name: 'ccc', 
    } 

最終數據結果

{ 
      _id:   '6d2847090a6a1b', 
      user_id:  '580da5653bd3cc5802f6c37c', 
      name:   'aaaa', 
      sum: 248 

     }, 

     { 
      _id:   '75f36d2847090a6a36580871', 
      user_id:  '3bd3cc5802f6c37c580da565', 
      name:   'bbbb', 
      sum :30 
     }, 
     { 
      _id:   '6a3087184367090a6a3e58fd284', 
      user_id:  '2f63bc580c37c580da565d3c', 
      name:   'ccc', 
      sum :99 
     } 
+1

這完全不清楚你要做什麼。 '一切工作正常,但創建對象後,我們需要減少總和值(減)' – Jamiec

+0

沿@Jamiec的行,你想創建一個新的對象列表,同時修改原始對象的列表,以減少總和每個條目的數量是爲新列表創建的對象的數量? –

回答

2

你可以從你的總和減去gifted_points

var data = [{ _id: '6d2847090a6a1b', user_id: '580da5653bd3cc5802f6c37c', name: 'aaaa', sum: 250 }, { _id: '75f36d2847090a6a36580871', user_id: '3bd3cc5802f6c37c580da565', name: 'bbbb', sum: 30 }, { _id: '6a3087184367090a6a3e58fd284', user_id: '2f63bc580c37c580da565d3c', name: 'ccc', sum: 100 }], 
 
    giftedObject = []; 
 

 
data.forEach(function (userSummary) { 
 
    var i, 
 
     gifted_point = Math.floor(userSummary.sum/100); 
 

 
    userSummary.sum -= gifted_point; 
 
    for (i = 0; i < gifted_point; i++) { 
 
     giftedObject.push({ 
 
      user_id: userSummary.user_id, 
 
      name: userSummary.name, 
 
      gifted_point: 1, 
 
      active: true 
 
     }); 
 
    } 
 
}); 
 

 
result = { giftedObject: giftedObject }; 
 
console.log(data); 
 
console.log(result);
.as-console-wrapper { max-height: 100% !important; top: 0; }

+0

我需要獲取兩個不同的對象,一個用於最終數據,一個用於giftedObject –

0

如何遞減的for循環裏面的總和?

for(var i = 0; i < userSummary.numberOfGifted; i++){ 
    giftedObject.push({ 
     user_id: userSummary.user_id, 
     name: userSummary.name, 
     gifted_point: 1, 
     active: true 
    }); 
    userSummary.sum -= 1; // <--- add this line 
}