user7035864
user7035864

Reputation:

how to merge JSON object together

i want to make a bar-chart and i have two functions each of them return data for exemple : the first function 1,:

$scope.data = {
    labels: ['Jan', 'Feb', 'Mar'],
    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: [55, 40, 84]
        }
    ]
};

the second function returned :

$scope.data = {
    labels: ['Apr', 'May', 'Jun', '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: [28, 48, 40, 19, 86, 27, 90, 102, 123]
        }
    ]
};

my quesion is how i can combine this resut to get ths final result:

 $scope.data = {
    labels: ['Jan', 'Feb', 'Mar','Apr', 'May', 'Jun', '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: [28, 48, 40, 19, 86, 27, 90, 102, 123]
        },
        {
            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: [28, 48, 40, 19, 86, 27, 90, 102, 123]
        }
    ]
};

Upvotes: 1

Views: 63

Answers (3)

Tom Johnson
Tom Johnson

Reputation: 689

You can do;

$scope.data1.datasets.push($scope.data2.datasets[0]);

I would advise against binding to the same array for seperate functions.

Upvotes: 0

devqon
devqon

Reputation: 13997

You can easily do something like this:

$scope.data.labels = $scope.data1.labels.concat($scope.data2.labels);
$scope.data.datasets = $scope.data1.datasets.concat($scope.data2.datasets);

In your case probably (assuming because you didn't include the functions):

$scope.data = { labels: [], datasets: [] };

getFirstDataSet(function(response) {
    concatToData(response);
});

getSecondDataSet(function(response) {
    concatToData(response);
});

function concatToData(response) {
    $scope.data.labels = $scope.data.labels.concat(response.labels);
    $scope.data.datasets = $scope.data.datasets.concat(response.datasets);
}

Upvotes: 1

Madhu
Madhu

Reputation: 2803

Use the concat method.

 var finalObj = data1.concat(data2);

Upvotes: 1

Related Questions