我们如何将json对象合并在一起

时间:2016-11-08 14:34:54

标签: angularjs json ionic-framework

我有我的第一个功能:

 $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 to push into 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 to GET DATA----
    ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess,onError);
                                          };

此函数返回此结果:

$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]
    }
]
                 };

效果很好。

并且我有第二个函数返回相同的结果,但有不同的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 to push into 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 to GET DATA----
ajaxServicess.getData(url,username,password, 'GET', '').then(onSuccess, onError);

                          };

此函数返回此结果:

$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]
    }
]
                             };

它的效果也不错,但我的问题是:如何在第一个函数中声明第二个函数并合并返回的数据并获得最终结果:

$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]
    }
]
                    };

2 个答案:

答案 0 :(得分:0)

只要您的对象只包含数组,您可以按如下方式解决此问题:

X = [[1,2,3],[1,2,3],[1,2,3]]

这个小提琴的演示:https://jsfiddle.net/gat4co9x/

希望这会有所帮助。 ;)

编辑:要集成,只需在第一个函数的onSuccess中调用第二个函数,在第二个函数的onSuccess中调用combineParts。

答案 1 :(得分:0)

这对您来说是一个更容易的更改,在准备$scope.loadDataFromToMonth(params);后,首先调用$scope.loadDataFromToMonthArrivee(params);函数并在成功调用第二函数$scope.data内调用该函数。

现在不要在$scope.data中再次初始化$scope.loadDataFromToMonthArrivee(params);,只需执行此操作$scope.data.datasets.push(dataSetObjtwo);

我在下面提供了类似的代码,所以只是尝试使用相同的不要混淆并尝试任何新的但坚持它,并在此工作后,您可以处理从两个对象分离标签列表。

$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 to push into 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);    
        $scope.loadDataFromToMonthArrivee(params);//call the second list http call after you prepare first object ...      
    }
    else {
        alert("failure");
    }
};
function onError(response) {
    console.log("-------getIncidentDepartByMonthFromTo FAILED-------");
    //$scope.stopSpin('spinner-0');
    console.log(response.data);
    console.log("Inside getIncidentDepartByMonthFromTo error condition...");
  };

$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));

        var theWholeOb={};
        var dataSetObj = {}; //temp object to push into 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...");
};