将原始JavaScript对象数组转换为结构化对象树,以便重复使用

我用node.js,mssql(不要问客户端),socket.io和angularjs构build一个应用程序。

我已经设法从以下forms从mssql到angular度的数据。

[ { "measure":"value", "region":"London", "manager":"Jack", "supervisor":"James", "number1":44, "number2":2244.3, "number3":1561.6 }, { "measure":"value", "region":"London", "manager":"Jack", "supervisor":"Jerry", "number1":335.4, "number2":33.3, "number3":11.6 }, { "measure":"value", "region":"London", "manager":"John", "supervisor":"Joseph", "number1":444.3, "number2":233, "number3":1561.6 } ] 

我试图通过angular.forEach来操作数据,以达到以下结果:

 [ { "region": "London", "regionTotals" : { "turnover" : { "number1" : "TOTAL OF NUMBER 1 WITH REGION LONDON", "number2" : "TOTAL OF NUMBER 2 WITH REGION LONDON", "number3" : "TOTAL OF NUMBER 3 WITH REGION LONDON" } }, "managers" : { "jack" : { "managerTotals" { "number1": "TOTAL OF NUMBER 1 WITH MANAGER JACK", "number2": "TOTAL OF NUMBER 2 WITH MANAGER JACK", "number3": "TOTAL OF NUMBER 3 WITH MANAGER JACK" }, "supervisors" : { "Jerry": { "supervisorTotals" : { "number1":335.4, "number2":33.3, "number3":11.6 } }, "James": { "supervisorTotals" : { "number1":44, "number2":2244.3, "number3":1561.6 } } } }, "john" : { "managerTotals" { "number1": "TOTAL OF NUMBER 1 WITH MANAGER JOHN", "number2": "TOTAL OF NUMBER 2 WITH MANAGER JOHN", "number3": "TOTAL OF NUMBER 3 WITH MANAGER JOHN" }, "supervisors" : { "Joseph": { "supervisorTotals" : { "number1":444.3, "number2":233, "number3":1561.6 } } } } } } ] 

这是做这个最好的方法吗? 如果你有任何关于如何通过JavaScript循环运行它的build议,将非常感激,因为我正在挣扎大时间。 尤其是试图设置对象键。

我能想到的唯一方法是在节点中运行一堆查询,并将其放在服务器端而不是客户端。 不过,考虑到我使用的是mssql,我想保持查询的最低限度。

谢谢

       

网上收集的解决方案 "将原始JavaScript对象数组转换为结构化对象树,以便重复使用"

我不会说这是最好的解决scheme,而是实现您的目标的解决scheme

我已经通过使用特定的迭代使用angular.forEachangular.forEachJSON扁平化为相应的结构。 $scope.flattenedRecord包含转换后的JSON

工作演示

 var app = angular.module('myApp', []); app.controller('ArrayController', function ($scope) { $scope.records = [{ "measure": "value", "region": "London", "manager": "Jack", "supervisor": "James", "number1": 44, "number2": 2244.3, "number3": 1561.6 }, { "measure": "value", "region": "London", "manager": "Jack", "supervisor": "Jerry", "number1": 335.4, "number2": 33.3, "number3": 11.6 }, { "measure": "value", "region": "London", "manager": "John", "supervisor": "Joseph", "number1": 444.3, "number2": 233, "number3": 1561.6 }]; $scope.flattenedRecord = []; $scope.managers = []; $scope.region = []; $scope.records.reduce(function (result, item) { $scope.managers.push(item.manager); $scope.region.push(item.region); }, 0); $scope.managers = _.uniq($scope.managers); $scope.region = _.uniq($scope.region); var mainRecordobj = {}; angular.forEach($scope.region, function (regionValue) { mainRecordobj.region = regionValue; var regionTurnoverObj = {}; var regionNumberObj = {}; var regionTotalsObj = {}; var supervisorTotalsObj = {}; var supervisorNumberObj = {}; var supervisorNameObj = {}; var managerTotalsObj = {}; var managerNameObj = {}; var managerNumberObj = {}; regionNumberObj.number1 = "TOTAL OF NUMBER 1 WITH REGION " + regionValue.toUpperCase(); regionNumberObj.number2 = "TOTAL OF NUMBER 2 WITH REGION " + regionValue.toUpperCase(); regionNumberObj.number3 = "TOTAL OF NUMBER 3 WITH REGION " + regionValue.toUpperCase(); angular.forEach($scope.managers, function (managerName) { managerNumberObj = {}; managerNumberObj.number1 = "TOTAL OF NUMBER 1 WITH MANAGER " + managerName.toUpperCase(); managerNumberObj.number2 = "TOTAL OF NUMBER 2 WITH MANAGER " + managerName.toUpperCase(); managerNumberObj.number3 = "TOTAL OF NUMBER 3 WITH MANAGER " + managerName.toUpperCase(); managerTotalsObj = {}; managerTotalsObj.managerTotals = managerNumberObj; supervisorNameObj = {}; angular.forEach($scope.records, function (recordArray, recordIndex) { if (managerName === recordArray.manager) { angular.forEach(recordArray, function (recordValue, recordKey) { if (recordValue === managerName) { supervisorNumberObj = {}; supervisorNumberObj.number1 = recordArray.number1; supervisorNumberObj.number2 = recordArray.number2; supervisorNumberObj.number3 = recordArray.number3; supervisorTotalsObj = {}; supervisorTotalsObj.supervisorTotals = supervisorNumberObj; supervisorNameObj[recordArray.supervisor] = supervisorTotalsObj; } }); } }); managerTotalsObj.supervisors = supervisorNameObj; managerNameObj[managerName.toLowerCase()] = managerTotalsObj; }); regionTurnoverObj.turnover = regionNumberObj; mainRecordobj.regionTotals = regionTurnoverObj; mainRecordobj.managers = managerNameObj; $scope.flattenedRecord.push(mainRecordobj); console.log(angular.toJson($scope.flattenedRecord)); }); }); 

OUTPUT

 [ { "region": "London", "regionTotals": { "turnover": { "number1": "TOTAL OF NUMBER 1 WITH REGION LONDON", "number2": "TOTAL OF NUMBER 2 WITH REGION LONDON", "number3": "TOTAL OF NUMBER 3 WITH REGION LONDON" } }, "managers": { "jack": { "managerTotals": { "number1": "TOTAL OF NUMBER 1 WITH MANAGER JACK", "number2": "TOTAL OF NUMBER 2 WITH MANAGER JACK", "number3": "TOTAL OF NUMBER 3 WITH MANAGER JACK" }, "supervisors": { "James": { "supervisorTotals": { "number1": 44, "number2": 2244.3, "number3": 1561.6 } }, "Jerry": { "supervisorTotals": { "number1": 335.4, "number2": 33.3, "number3": 11.6 } } } }, "john": { "managerTotals": { "number1": "TOTAL OF NUMBER 1 WITH MANAGER JOHN", "number2": "TOTAL OF NUMBER 2 WITH MANAGER JOHN", "number3": "TOTAL OF NUMBER 3 WITH MANAGER JOHN" }, "supervisors": { "Joseph": { "supervisorTotals": { "number1": 444.3, "number2": 233, "number3": 1561.6 } } } } } } ]