我正在使用node.js,mssql(不要问...客户端),socket.io和angularjs构建应用程序。
我已设法以下列形式从mssql获取数据。
[
{
"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循环运行它有任何建议,我将非常感激,因为我正在努力奋斗。特别是试图设置对象键。
我能想到的另一种方法是在节点中运行一堆查询并将其放在服务器端而不是客户端。但是考虑到我使用mssql,我希望将查询保持在最低限度。
由于
答案 0 :(得分:1)
我不会说这是最好的解决方案,而是实现目标的解决方案
通过使用JSON
和.reduce
的特定迭代,我已将angular.forEach
展平为相应的结构。 $scope.flattenedRecord
包含已转换的JSON
<强> Working Demo 强>
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));
});
});
<强>输出强>
[
{
"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
}
}
}
}
}
}
]