Estoy tratando de optimizar una solución que se usa para agrupar los objetos según el valor presente en otra matriz ( mapping ) que está escrita como NodeJS.
function workerFunction(report) { const groupedArray = [] const granularityKey = 'Month' const mapping = ['TestField1', 'TestField2'] //will be dynamic based on json input let activeIIndex = 0; while (activeIIndex < report.length) { const index = groupedArray .findIndex(item => mapping .every((column) => item[column] === report[activeIIndex][column])) if (index === -1) { report[activeIIndex][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost'] groupedArray.push(report[activeIIndex]) } else { groupedArray[index][report[activeIIndex][granularityKey]] = report[activeIIndex]['Cost'] } activeIIndex++; } return groupedArray } La entrada a la función del workerFunction(report) se verá así (por lo general, el tamaño de la matriz será superior a 300k),
[ { "TestField1":"value", "TestField2":"value2", "Cost":12.5555, "Month":10 }, { "TestField1":"value3", "TestField2":"value4", "Cost":142.5555, "Month":10 }, { "TestField1":"value6", "TestField2":"value4", "Cost":15.87, "Month":10 }, { "TestField1":"value3", "TestField2":"value4", "Cost":16.5555, "Month":11 } ] El resultado esperado después de pasar este objeto json a través workerFunction será
[ { "TestField1":"value", "TestField2":"value2", "Cost":12.5555, "Month":10, "10":12.5555 }, { "TestField1":"value3", "TestField2":"value4", "Cost":142.5555, "Month":10, "10":142.5555, "11":16.5555 }, { "TestField1":"value6", "TestField2":"value4", "Cost":15.87, "Month":10, "10":15.87 }, ]