Convert raw javascript object array into structured object tree ready for ng-repeat -



Convert raw javascript object array into structured object tree ready for ng-repeat -

i building app node.js, mssql (don't ask.. client), socket.io , angularjs.

i have managed info mssql angular in next form.

[ { "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 } ]

i trying manipulate info through angular.foreach accomplish next result:

[ { "region": "london", "regiontotals" : { "turnover" : { "number1" : "total of number 1 part london", "number2" : "total of number 2 part london", "number3" : "total of number 3 part london" } }, "managers" : { "jack" : { "managertotals" { "number1": "total of number 1 manager jack", "number2": "total of number 2 manager jack", "number3": "total of number 3 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 manager john", "number2": "total of number 2 manager john", "number3": "total of number 3 manager john" }, "supervisors" : { "joseph": { "supervisortotals" : { "number1":444.3, "number2":233, "number3":1561.6 } } } } } } ]

is best way this? , if have advice on how run through javascript loop much appreciated i'm struggling big time. trying set object keys.

the other way can think of doing running bunch of queries in node , putting server side rather client side. considering i'm using mssql wanted maintain queries bare minimum.

thanks

i wont best solution rather solution achieves goal

i have flattened json corresponding construction using iteration .reduce , angular.foreach. $scope.flattenedrecord contains converted 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 part " + regionvalue.touppercase(); regionnumberobj.number2 = "total of number 2 part " + regionvalue.touppercase(); regionnumberobj.number3 = "total of number 3 part " + regionvalue.touppercase(); angular.foreach($scope.managers, function (managername) { managernumberobj = {}; managernumberobj.number1 = "total of number 1 manager " + managername.touppercase(); managernumberobj.number2 = "total of number 2 manager " + managername.touppercase(); managernumberobj.number3 = "total of number 3 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 part london", "number2": "total of number 2 part london", "number3": "total of number 3 part london" } }, "managers": { "jack": { "managertotals": { "number1": "total of number 1 manager jack", "number2": "total of number 2 manager jack", "number3": "total of number 3 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 manager john", "number2": "total of number 2 manager john", "number3": "total of number 3 manager john" }, "supervisors": { "joseph": { "supervisortotals": { "number1": 444.3, "number2": 233, "number3": 1561.6 } } } } } } ]

javascript arrays node.js angularjs dynamic-arrays

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -