2021年3月28日星期日

Is there an algorithm for merging elements of an array of objects with the same key

I have the following data, and I've been looking for a reference to an algorithm for building a returned array that is grouped by a field (e.g. discipline) and pushes in all the unique fields.

Initial Dataset:

[              { discipline: 1, originalBudgetDollars: 1114.96 },        { discipline: 2, originalBudgetDollars: 192 },        { discipline: 1, expendedDollars: 1137.24 },        { discipline: 2, expendedDollars: 55.08 },        { discipline: 1, forecastAtCompletionDollars: 1276.62 },        { discipline: 2, forecastAtCompletionDollars: 96 },        { discipline: 1, earnedValueDollars: 81.6 },        { discipline: 2, earnedValueDollars: 10636.6 }  ]  

Desired Result:

[         { discipline: 1, originalBudgetDollars: 1114.96, expendedDollars: 1137.24, remainingDollars: -22.28, forecastAtCompletionDollars: 1276.62, earnedValueDollars: 81.6 },        { discipline: 2, originalBudgetDollars: 192, expendedDollars: 55.08,  remainingDollars: 136.92, forecastAtCompletionDollars: 96, earnedValueDollars: 10636.6 },  ]  
https://stackoverflow.com/questions/66847613/is-there-an-algorithm-for-merging-elements-of-an-array-of-objects-with-the-same March 29, 2021 at 09:04AM

没有评论:

发表评论