imsi imsi imsi imsi - 1 month ago 12
JSON Question

how to use push inside a loop

the following function return this json object:


[{"number":2,"des":"Ceintures De Sécurités Passagères","la_date":1506960648000},{"number":7,"des":"Ceintures De Sécurité Conducteur","la_date":1509542654000},{"number":1,"des":"Ceintures De Sécurités Passagères","la_date":1512134781000}]


the function:

$scope.loadDataFromToMonth= function (from,to,year) {
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.labels = [];
// charCtrl.toto = [];
// $scope.data = {};
//$scope.qsd.push('ff');
getIncidentDepartByMonthFromTo.forEach(function(data) {
// charCtrl.toto.push($filter('monthName')(monthNumber));
var monthNumber=$filter('date')(data.la_date, "MM");
//$scope.labelsf.push($filter('monthName')(monthNumber));
var mun=data.number;
//$scope.dataf.push(data.number);
$scope.data = {
labels: [monthNumber],
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: [data.number]
}

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


My problem is , when the data apprears in bar char,just the last json object appears which is:


{"number":1,"des":"Ceintures De Sécurités Passagères","la_date":1512134781000}

Answer

Basically you need create a specific object and assign properties with values to the object like below :

$scope.data = {}; // new object 
$scope.data.datasets = []; // new array in data object ..
$scope.data.labels =[];
getIncidentDepartByMonthFromTo.forEach(function(data) {

    var monthNumber = $filter('date')(data.la_date, "MM");

    var mun = data.number;


    $scope.data.labels.push(monthNumber);

    var dataSetObj = {}; //temp object to push into dataset array..
    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)';
    dataSetObj.data.push(data.number);

    $scope.data.datasets.push(dataSetObj);

});