I am new to JSON and I am preparing an JSON object on client side from the response received from server. I want the data looks like below
[
{"number" : "456" ,
"effort" : [{
"effort_type" : "A",
"act_effort" : 10 },
{"effort_type" : "B",
"act_effort" : 20 }]
},
{"number" : "123" ,
"effort" : [{
"effort_type" : "A",
"act_effort" : 5},
{"effort_type" : "B",
"act_effort" : 30},
{"effort_type" : "C",
"act_effort" : 15}]
}
]
I am creating this JSON from the response received from server as below
$http.get('/colist').success(function(response) {
//console.log ("i GET the data of projects");
$scope.project = response;
}
Below is the code to prepare my second json object
var actEffortData = [];
var dataSource = $scope.project;
for(i=0; i<dataSource.length; i++){
var dupCoFound="N";
var currentNumber=dataSource[i].number;
var currentEffort = parseInt(dataSource[i].act_effort);
var currentEffortType= dataSource[i].effort_type;
if(i==0){
actEffortData.push({
number : currentNumber,
effort : {
effort_type:currentEffortType,
act_effort:currentEffort
}
});
} else {
for(k=0; k < actEffortData.length; k++){
var dupCEffFound="N";
if(actEffortData[k].number == currentNumber){
for(n=0; n < actEffortData[0].effort.length; n++){
if(actEffortData[k].effort[n].effort_type == currentEffortType){
var currentEffortTemp = actEffortData[k].effort[n].act_effort + currentEffort;
actEffortData[k].effort[n].act_effort = currentEffortTemp;
dupCEffFound="Y";
break;
}
}
if(dupCEffFound == "N"){
var tempActEffort =[];
tempActEffort = actEffortData[k].effort;
tempActEffort.push({
effort_type:currentEffortType,
act_effort:currentEffort
});
}
dupCoFound = "Y";
break;
}
}
if (dupCoFound=="N"){
actEffortData.push({
number : currentNumber,
effort : {
effort_type:currentEffortType,
act_effort:currentEffort
}
});
}
}
I am getting an error as below:
Error: tempActEffort.push is not a function
Can anyone show me how to achieve this one? Please note that actual code has functionality more than the above one, so you might find redundant variables usage.
Thanks in advance.