0

I'm trying to manipulate my response data in a way that each set of data group as a array 'years. So i made a loop to check every correspondent value associate by it Year, to group like.:

vm['years'][0] = (Array(12)),

vm['years'][1] = (Array(12)),

vm['years'][2] = (Array(8)),

vm['years'][3] = (Array(5)),

...

So theres my script:

<script type="text/javascript">

var response = [{"0":"1201","id":"1201","1":"15","year":"15","2":"1","month":"1","3":"87871.40","value":"87871.40"},
{"0":"1301","id":"1301","1":"15","year":"15","2":"2","month":"2","3":"83805.30","value":"83805.30"},
{"0":"1401","id":"1401","1":"15","year":"15","2":"3","month":"3","3":"113005.35","value":"113005.35"},
{"0":"1501","id":"1501","1":"15","year":"15","2":"4","month":"4","3":"79702.54","value":"79702.54"},
{"0":"1601","id":"1601","1":"15","year":"15","2":"5","month":"5","3":"53933.28","value":"53933.28"},
{"0":"1701","id":"1701","1":"15","year":"15","2":"6","month":"6","3":"55694.73","value":"55694.73"},
{"0":"1801","id":"1801","1":"15","year":"15","2":"7","month":"7","3":"60076.39","value":"60076.39"},
{"0":"1901","id":"1901","1":"15","year":"15","2":"8","month":"8","3":"39968.58","value":"39968.58"},
{"0":"2001","id":"2001","1":"15","year":"15","2":"9","month":"9","3":"68237.48","value":"68237.48"},
{"0":"2101","id":"2101","1":"15","year":"15","2":"10","month":"10","3":"49945.25","value":"49945.25"},
{"0":"2201","id":"2201","1":"15","year":"15","2":"11","month":"11","3":"37939.96","value":"37939.96"},
{"0":"2301","id":"2301","1":"15","year":"15","2":"12","month":"12","3":"68602.92","value":"68602.92"},
{"0":"2401","id":"2401","1":"16","year":"16","2":"1","month":"1","3":"64111.01","value":"64111.01"},
{"0":"2501","id":"2501","1":"16","year":"16","2":"2","month":"2","3":"40562.66","value":"40562.66"},
{"0":"2601","id":"2601","1":"16","year":"16","2":"3","month":"3","3":"68715.04","value":"68715.04"},
{"0":"2701","id":"2701","1":"16","year":"16","2":"4","month":"4","3":"57962.40","value":"57962.40"},
{"0":"2801","id":"2801","1":"16","year":"16","2":"5","month":"5","3":"87665.62","value":"87665.62"},
{"0":"2901","id":"2901","1":"16","year":"16","2":"6","month":"6","3":"85806.47","value":"85806.47"},
{"0":"3001","id":"3001","1":"16","year":"16","2":"7","month":"7","3":"65287.26","value":"65287.26"},
{"0":"3101","id":"3101","1":"16","year":"16","2":"8","month":"8","3":"80278.92","value":"80278.92"},
{"0":"3201","id":"3201","1":"16","year":"16","2":"9","month":"9","3":"55346.88","value":"55346.88"},
{"0":"3301","id":"3301","1":"16","year":"16","2":"10","month":"10","3":"90976.51","value":"90976.51"},
{"0":"3401","id":"3401","1":"16","year":"16","2":"11","month":"11","3":"82498.67","value":"82498.67"},
{"0":"3501","id":"3501","1":"16","year":"16","2":"12","month":"12","3":"81647.70","value":"81647.70"},
{"0":"3601","id":"3601","1":"17","year":"17","2":"1","month":"1","3":"55268.20","value":"55268.20"},
{"0":"3701","id":"3701","1":"17","year":"17","2":"2","month":"2","3":"62801.43","value":"62801.43"},
{"0":"3801","id":"3801","1":"17","year":"17","2":"3","month":"3","3":"52195.21","value":"52195.21"},
{"0":"3901","id":"3901","1":"17","year":"17","2":"4","month":"4","3":"79582.08","value":"79582.08"},
{"0":"4001","id":"4001","1":"17","year":"17","2":"5","month":"5","3":"98641.86","value":"98641.86"},
{"0":"4101","id":"4101","1":"17","year":"17","2":"6","month":"6","3":"96766.57","value":"96766.57"},
{"0":"4201","id":"4201","1":"17","year":"17","2":"7","month":"7","3":"81868.95","value":"81868.95"},
{"0":"4301","id":"4301","1":"17","year":"17","2":"8","month":"8","3":"88690.11","value":"88690.11"},
{"0":"45476","id":"45476","1":"18","year":"18","2":"1","month":"1","3":"344343.00","value":"344343.00"},
{"0":"45477","id":"45477","1":"18","year":"18","2":"2","month":"2","3":"23242.00","value":"23242.00"}]

vm = [];

vm['years'] = ['15','16', '17', '18'];
x = 0;
i = 0;
vm.years.forEach(function (currentValue, index) {

    // debugger;
        while (vm['years'][i] == response[x].year) {
            x++;         
        }
    i++;    
});

</script>

The response has 34 lines, but when i hit response[35].year i get:

Uncaught TypeError: Cannot read property 'year' of undefined

I tried to make a 'if' statement before the 'while' like response[x].year === undefined and response[x].year === null and response[x].hasOwnProperty(year) but didn't succeeded. I couldn't find better away to get the result the way i want and I'll appreciate any help to make a consistent logic.

1
  • 2
    if the response has 34 lines, then you should get an error when you try response[34].year Commented Jan 17, 2018 at 1:23

2 Answers 2

2

The error comes from the fact that response[x] is undefined. Using response[x].year is like accessing undefined.year which is an error, so using:

if ( response[x].year === undefined ) 

as a guard also causes the same error because you are still trying to access response[x].year. You should use this as a guard instead:

if ( response[x] === undefined ) 
Sign up to request clarification or add additional context in comments.

1 Comment

Yes. this helped me fix the problem for now :)
1

Personally, I would do something this instead:

var response = [{"0":"1201","id":"1201","1":"15","year":"15","2":"1","month":"1","3":"87871.40","value":"87871.40"},
{"0":"1301","id":"1301","1":"15","year":"15","2":"2","month":"2","3":"83805.30","value":"83805.30"},
{"0":"1401","id":"1401","1":"15","year":"15","2":"3","month":"3","3":"113005.35","value":"113005.35"},
{"0":"1501","id":"1501","1":"15","year":"15","2":"4","month":"4","3":"79702.54","value":"79702.54"},
{"0":"1601","id":"1601","1":"15","year":"15","2":"5","month":"5","3":"53933.28","value":"53933.28"},
{"0":"1701","id":"1701","1":"15","year":"15","2":"6","month":"6","3":"55694.73","value":"55694.73"},
{"0":"1801","id":"1801","1":"15","year":"15","2":"7","month":"7","3":"60076.39","value":"60076.39"},
{"0":"1901","id":"1901","1":"15","year":"15","2":"8","month":"8","3":"39968.58","value":"39968.58"},
{"0":"2001","id":"2001","1":"15","year":"15","2":"9","month":"9","3":"68237.48","value":"68237.48"},
{"0":"2101","id":"2101","1":"15","year":"15","2":"10","month":"10","3":"49945.25","value":"49945.25"},
{"0":"2201","id":"2201","1":"15","year":"15","2":"11","month":"11","3":"37939.96","value":"37939.96"},
{"0":"2301","id":"2301","1":"15","year":"15","2":"12","month":"12","3":"68602.92","value":"68602.92"},
{"0":"2401","id":"2401","1":"16","year":"16","2":"1","month":"1","3":"64111.01","value":"64111.01"},
{"0":"2501","id":"2501","1":"16","year":"16","2":"2","month":"2","3":"40562.66","value":"40562.66"},
{"0":"2601","id":"2601","1":"16","year":"16","2":"3","month":"3","3":"68715.04","value":"68715.04"},
{"0":"2701","id":"2701","1":"16","year":"16","2":"4","month":"4","3":"57962.40","value":"57962.40"},
{"0":"2801","id":"2801","1":"16","year":"16","2":"5","month":"5","3":"87665.62","value":"87665.62"},
{"0":"2901","id":"2901","1":"16","year":"16","2":"6","month":"6","3":"85806.47","value":"85806.47"},
{"0":"3001","id":"3001","1":"16","year":"16","2":"7","month":"7","3":"65287.26","value":"65287.26"},
{"0":"3101","id":"3101","1":"16","year":"16","2":"8","month":"8","3":"80278.92","value":"80278.92"},
{"0":"3201","id":"3201","1":"16","year":"16","2":"9","month":"9","3":"55346.88","value":"55346.88"},
{"0":"3301","id":"3301","1":"16","year":"16","2":"10","month":"10","3":"90976.51","value":"90976.51"},
{"0":"3401","id":"3401","1":"16","year":"16","2":"11","month":"11","3":"82498.67","value":"82498.67"},
{"0":"3501","id":"3501","1":"16","year":"16","2":"12","month":"12","3":"81647.70","value":"81647.70"},
{"0":"3601","id":"3601","1":"17","year":"17","2":"1","month":"1","3":"55268.20","value":"55268.20"},
{"0":"3701","id":"3701","1":"17","year":"17","2":"2","month":"2","3":"62801.43","value":"62801.43"},
{"0":"3801","id":"3801","1":"17","year":"17","2":"3","month":"3","3":"52195.21","value":"52195.21"},
{"0":"3901","id":"3901","1":"17","year":"17","2":"4","month":"4","3":"79582.08","value":"79582.08"},
{"0":"4001","id":"4001","1":"17","year":"17","2":"5","month":"5","3":"98641.86","value":"98641.86"},
{"0":"4101","id":"4101","1":"17","year":"17","2":"6","month":"6","3":"96766.57","value":"96766.57"},
{"0":"4201","id":"4201","1":"17","year":"17","2":"7","month":"7","3":"81868.95","value":"81868.95"},
{"0":"4301","id":"4301","1":"17","year":"17","2":"8","month":"8","3":"88690.11","value":"88690.11"},
{"0":"45476","id":"45476","1":"18","year":"18","2":"1","month":"1","3":"344343.00","value":"344343.00"},
{"0":"45477","id":"45477","1":"18","year":"18","2":"2","month":"2","3":"23242.00","value":"23242.00"}
];

function HandleResponse(responseArray){
  this.responseArray = responseArray;
  this.getArrayByYear = function(){
    var a = [].slice.call(arguments), r = this.responseArray, ra = [];
    for(var i=0,l=a.length; i<l; i++){
      ra.push([]);
      for(var n=0,o,c=r.length; n<c; n++){
        o = r[n];
        if(+a[i] === +o.year){
          ra[i].push(o);
        }
      }
    }
    return ra;
  }
}

var resp = new HandleResponse(response);
console.log(resp.getArrayByYear(15, 16, 17, 18)); //send years

1 Comment

Amazing! I trying to understand this method for now thanks! and actually theres a little error, isnt showing the first month for the year 15. This value is being skipped: {"0":"1201","id":"1201","1":"15","year":"15","2":"1","month":"1","3":"87871.40","value":"87871.40"}

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.