2017-02-21 69 views
2

我有一個對象,基本上有三個重要參數需要進行總計算。 因此,例如,我的目標是如下:從基於數組的對象計算求和值並將其與總數進行更新

[ 
{'Id':1,'ResourceId':1,'StartDate':'01-01-2017','Hours':8,'TotalHrs':8}, 
{'Id':2,'ResourceId':1,'StartDate':'01-01-2017','Hours':4,'TotalHrs':4}, 
{'Id':3,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':4}, 
{'Id':4,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':4}, 
{'Id':5,'ResourceId':2,'StartDate':'01-01-2017','Hours':2,'TotalHrs':2}, 
{'Id':6,'ResourceId':2,'StartDate':'01-01-2017','Hours':4,'TotalHrs':4}, 
{'Id':7,'ResourceId':2,'StartDate':'01-03-2017','Hours':2,'TotalHrs':2}, 
] 

所以,目前TotalHrs參數是相同的值隨着時間的參數。

我希望它是一個特定的ResourceId和Startdate的總數。 即如果在數組中如果ResourceId和Startdate匹配TotalHrs參數應該是Hours參數的總和。

所以最後的數組變成了。

[ 
{'Id':1,'ResourceId':1,'StartDate':'01-01-2017','Hours':8,'TotalHrs':12}, 
{'Id':2,'ResourceId':1,'StartDate':'01-01-2017','Hours':4,'TotalHrs':12}, 
{'Id':3,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':8}, 
{'Id':4,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':8}, 
{'Id':5,'ResourceId':2,'StartDate':'01-01-2017','Hours':2,'TotalHrs':6}, 
{'Id':6,'ResourceId':2,'StartDate':'01-01-2017','Hours':4,'TotalHrs':6}, 
{'Id':7,'ResourceId':2,'StartDate':'01-03-2017','Hours':2,'TotalHrs':2}, 
] 

因此,在上述例子中,ResourceId:1和StartDate:2017年1月1日具有兩次出現的與時間值作爲8和4,所以有TotalHrs變爲8 + 4 = 12。

此外,我不想分組,我想在結果數組中相同數量的元素,但具有TotalHrs的更新值。

使用 Array.prototype.reduce()Array.prototype.forEach()功能
+0

值等' '的StartDate'='01-01-2017''是無效 – RomanPerekhrest

+0

可能的複製[什麼是GROUPBY對象的JavaScript的陣列上的最有效的方法?] (http://stackoverflow.com/questions/14446511/what-is-the-most-efficient-method-to-groupby-on-a-javascript-array-of-objects) – George

+0

@RomanPerekhrest,它不是日期格式確切地說,它的String.I確實需要爲了應用而以這種方式使用它。 – Mukta

回答

0

簡短的解決方案:

var data = [ {'Id':1,'ResourceId':1,'StartDate':'01-01-2017','Hours':8,'TotalHrs':8}, {'Id':2,'ResourceId':1,'StartDate':'01-01-2017','Hours':4,'TotalHrs':4}, {'Id':3,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':4}, {'Id':4,'ResourceId':1,'StartDate':'01-03-2017','Hours':4,'TotalHrs':4}, {'Id':5,'ResourceId':2,'StartDate':'01-01-2017','Hours':2,'TotalHrs':2}, {'Id':6,'ResourceId':2,'StartDate':'01-01-2017','Hours':4,'TotalHrs':4}, {'Id':7,'ResourceId':2,'StartDate':'01-03-2017','Hours':2,'TotalHrs':2} ]; 
 

 
data.forEach(function (o) { 
 
    o.TotalHrs = this[o.ResourceId + o.StartDate]; 
 
}, data.reduce(function (r, o) { 
 
    var k = o.ResourceId + o.StartDate; 
 
    (!r[k])? r[k] = o.TotalHrs : r[k] += o.TotalHrs; 
 
    return r; 
 
}, {})); 
 

 
console.log(data);

var k = o.ResourceId + o.StartDate; - k這裏是分組具有相同ResourceIdStartDate條目散列密鑰字符串屬性

0

我建議使用一個時間ary對象,使用StartDateResourceId作爲散列鍵,它將存儲小時。這種算法提供者O(n)的時間複雜度。

var data = [{'Id':1,'ResourceId':1,'StartDate':'01-01-2017','Hours':8},{'Id':2,'ResourceId':1,'StartDate':'01-01-2017','Hours':4},{'Id':3,'ResourceId':1,'StartDate':'01-03-2017','Hours':4},{'Id':4,'ResourceId':1,'StartDate':'01-03-2017','Hours':4},{'Id':5,'ResourceId':2,'StartDate':'01-01-2017','Hours':2},{'Id':6,'ResourceId':2,'StartDate':'01-01-2017','Hours':4},{'Id':7,'ResourceId':2,'StartDate':'01-03-2017','Hours':2}]; 
 

 
function populateTotalHrs(collection) { 
 
    var collectionByHash = {}; 
 
    collection.forEach(function(obj) { 
 
    var hash = obj.ResourceId + '_' + obj.StartDate; 
 
    collectionByHash[hash] = collectionByHash[hash] || 0; 
 
    collectionByHash[hash] += obj.Hours; 
 
    }); 
 
    collection.forEach(function(obj) { 
 
    var hash = obj.ResourceId + '_' + obj.StartDate; 
 
    obj.TotalHrs = collectionByHash[hash]; 
 
    }); 
 
} 
 

 
populateTotalHrs(data); 
 
console.log(data);

0

你可以把它暫時由ResourceIdStartDate使用哈希表和組。

var data = [{ Id: 1, ResourceId: 1, StartDate: '01-01-2017', Hours: 8, TotalHrs: 8 }, { Id: 2, ResourceId: 1, StartDate: '01-01-2017', Hours: 4, TotalHrs: 4 }, { Id: 3, ResourceId: 1, StartDate: '01-03-2017', Hours: 4, TotalHrs: 4 }, { Id: 4, ResourceId: 1, StartDate: '01-03-2017', Hours: 4, TotalHrs: 4 }, { Id: 5, ResourceId: 2, StartDate: '01-01-2017', Hours: 2, TotalHrs: 2 }, { Id: 6, ResourceId: 2, StartDate: '01-01-2017', Hours: 4, TotalHrs: 4 }, { Id: 7, ResourceId: 2, StartDate: '01-03-2017', Hours: 2, TotalHrs: 2 }]; 
 

 
data.forEach(function (hash) { 
 
    return function (a) { 
 
     var key = [a.ResourceId, a.StartDate].join('|'); 
 
     if (!hash[key]) { 
 
      hash[key] = [a]; 
 
      return; 
 
     } 
 
     hash[key].push(a); 
 
     hash[key][0].TotalHrs += a.Hours; 
 
     hash[key].forEach(function (b) { 
 
      b.TotalHrs = hash[key][0].TotalHrs; 
 
     }); 
 
    }; 
 
}(Object.create(null))); 
 

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

相關問題