angularjs - How we can merge json object together -
i have first function:
$scope.loaddatafromtomonth= function (from,to,year) { // $scope.loaddatafromtomontharrivee(from,to,2016); var url = servername+'admin/dashboard/getincidentdepartbymonthfromto/'+from+'/'+to+'/'+year; // alert(url); function onsuccess(response) { console.log("+++++getincidentdepartbymonthfromto success++++++"); if (response.data.success != false) { $scope.payloadgetincidentdepartbymonthfromto = response.data.data; var getincidentdepartbymonthfromto= $scope.payloadgetincidentdepartbymonthfromto; console.log(json.stringify(getincidentdepartbymonthfromto)); $scope.data = {}; // new object $scope.data.datasets = []; // new array in data object .. $scope.data.labels =[]; var thewholeob={}; var datasetobj = {}; //temp object push dataset array.. var datasetobjtwo = {}; /////////////anomalies depart datasetobj.data = []; datasetobj.label= 'my first dataset'; datasetobj.fillcolor='rgba(220,220,220,0.2)'; datasetobj.strokecolor= 'rgba(220,220,220,1)'; datasetobj.pointcolor= 'rgba(220,220,220,1)'; datasetobj.pointstrokecolor= '#fff'; datasetobj.pointhighlightfill= '#fff'; datasetobj.pointhighlightstroke='rgba(220,220,220,1)'; getincidentdepartbymonthfromto.foreach(function(data) { var monthnumber = $filter('date')(data.la_date, "mm"); var mun = data.number; $scope.data.labels.push(monthnumber); datasetobj.data.push(mun); }); $scope.data.datasets.push(datasetobj); } else { alert("failure"); } }; function onerror(response) { console.log("-------getincidentdepartbymonthfromto failed-------"); //$scope.stopspin('spinner-0'); console.log(response.data); console.log("inside getincidentdepartbymonthfromto error condition..."); }; //----make ajax request call data---- ajaxservicess.getdata(url,username,password, 'get', '').then(onsuccess,onerror); };
this function return result:
$scope.data = { labels: ['jan', 'feb' 'jul'], datasets: [ { label: 'my first dataset', fillcolor: 'rgba(220,220,220,0.2)', strokecolor: 'rgba(220,220,220,1)', pointcolor: 'rgba(220,220,220,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(220,220,220,1)', data: [75, 59, 80, 81, 56, 55] } ] };
it works good.
and have second function return same result different data of cours:
$scope.loaddatafromtomontharrivee= function (from,to,year) { var url =servername+'admin/dashboard/getincidentarriveebymonthfromto/'+from+'/'+to+'/'+year; //alert(url); function onsuccess(response) { console.log("+++++getincidentarriveebydate success++++++"); if (response.data.success != false) { $scope.payloaddaymonthyeardata = response.data.data; var loadeddatabydaymonthyear= $scope.payloaddaymonthyeardata; alert('xxx'+json.stringify(loadeddatabydaymonthyear)); $scope.data = {}; // new object $scope.data.datasets = []; // new array in data object .. $scope.data.labels =[]; var thewholeob={}; var datasetobj = {}; //temp object push dataset array.. var datasetobjtwo = {}; /////////////anomalies arrivee datasetobjtwo.data = []; $scope.date=[]; datasetobjtwo.label='my second dataset'; datasetobjtwo.fillcolor= 'rgba(151,187,205,0.2)'; datasetobjtwo.strokecolor= 'rgba(151,187,205,1)'; datasetobjtwo.pointcolor= 'rgba(151,187,205,1)'; datasetobjtwo.pointstrokecolor= '#fff'; datasetobjtwo.pointhighlightfill='#fff'; datasetobjtwo.pointhighlightstroke= 'rgba(151,187,205,1)'; loadeddatabydaymonthyear.foreach(function(data) { var monthnumber = $filter('date')(data.la_date, "mm"); $scope.date.push(monthnumber); var mun = data.number; $scope.data.labels.push($scope.monthnumber); datasetobjtwo.data.push(mun); }); $scope.data.datasets.push(datasetobjtwo); } else { alert("failure"); } // $scope.stopspin('spinner-0'); }; function onerror(response) { console.log("-------getincidentarriveebydate failed-------"); //$scope.stopspin('spinner-0'); console.log(response.data); console.log("inside getincidentarriveebydate error condition..."); }; //----make ajax request call data---- ajaxservicess.getdata(url,username,password, 'get', '').then(onsuccess, onerror); };
this function return result:
$scope.data = { labels: [ 'jan', 'feb' 'jul','jul', 'aug', 'sep', 'oct', 'nov', 'dec'], datasets: [ { label: 'my second dataset', fillcolor: 'rgba(151,187,205,0.2)', strokecolor: 'rgba(151,187,205,1)', pointcolor: 'rgba(151,187,205,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(151,187,205,1)', data: [ 102, 123, 145, 60, 161] } ] };
it works also, question : how can declare second function inside first function , combine data returned , final result this:
$scope.data = { labels: [ 'jul', 'aug', 'sep', 'oct', 'nov', 'dec'], datasets: [ { label: 'my second dataset', fillcolor: 'rgba(151,187,205,0.2)', strokecolor: 'rgba(151,187,205,1)', pointcolor: 'rgba(151,187,205,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(151,187,205,1)', data: [ 102, 123, 145, 60, 161] },{ label: 'my first dataset', fillcolor: 'rgba(220,220,220,0.2)', strokecolor: 'rgba(220,220,220,1)', pointcolor: 'rgba(220,220,220,1)', pointstrokecolor: '#fff', pointhighlightfill: '#fff', pointhighlightstroke: 'rgba(220,220,220,1)', data: [75, 59, 80, 81, 56, 55] } ] };
as long objects contain arrays might solve follows:
$scope.combineparts = function(part1, part2) { var merged = angular.copy( part1 ); ( key in part1 ) { if ( part2.hasownproperty(key) ) merged[key].push.apply(merged[key], part2[key]) } return merged; }
demo in fiddle: https://jsfiddle.net/gat4co9x/
hope helps. ;)
edit: integrate call second function in onsuccess of first, , combineparts in onsuccess of second.
Comments
Post a Comment