0

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.

3
  • see line 'tempActEffort = actEffortData[k].effort;' no longer allows your variable 'tempActEffort' to act as array…! Commented Mar 25, 2015 at 9:04
  • what if in your server side you already prepare the json data to your client side when the response is receive by the client server.. just a suggestion Commented Mar 25, 2015 at 9:05
  • @Oli Soproni B. I need multiple JSON objects, each have different structure based on few things to be mapped. I can't prepare jason data on server side. Commented Mar 25, 2015 at 11:53

2 Answers 2

1

I observed one thing in your code at:

var tempActEffort =[];
tempActEffort = actEffortData[k].effort;

Here, you declared a variable 'tempActEffort' as an array.Then you are directly initializing some value as 'tempActEffort = actEffortData[k].effort' to it so that its type got changed from array to resulting type. So, then push is not a function defined on it as it is not an array type at the moment.

So, instead of assigning value as 'tempActEffort = actEffortData[k].effort' change it as:

tempActEffort.push(actEffortData[k].effort);
1
  • I changed as you suggested. The error is gone, but it is not working still. I have added few console.log() statements to check. effort values are coming as undefined. Can you please help me with it ? Here is the fiddle jsfiddle.net/DivB/gwkrg81a/4 Thanks in advance Commented Mar 25, 2015 at 12:15
0

Check if

tempActEffort = actEffortData[k].effort;

returned an array object. Push can be applied only to array

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.