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
}
這完全不清楚你要做什麼。 '一切工作正常,但創建對象後,我們需要減少總和值(減)' – Jamiec
沿@Jamiec的行,你想創建一個新的對象列表,同時修改原始對象的列表,以減少總和每個條目的數量是爲新列表創建的對象的數量? –