Javascript Uncaught TypeError:无法读取undefined的属性

时间:2018-01-17 01:18:37

标签: javascript arrays json

我试图以每组数据组作为数组的方式操纵我的响应数据。 所以我做了一个循环来检查每一个相关联的年份的相应值,如同组:

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

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

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

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

...

所以我的剧本:

<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>

响应有34行,但当我点击response[35].year时,我得到:

  

未捕获的TypeError:无法读取属性&#39;年份&#39;未定义的

我试图制作一个&#39; if&#39;在&#39;之前的陈述&#39;例如response[x].year === undefinedresponse[x].year === null以及response[x].hasOwnProperty(year),但没有成功。 我无法找到更好的结果,以我想要的方式获得结果,并且我会感谢任何帮助以制定一致的逻辑。

2 个答案:

答案 0 :(得分:1)

错误来自response[x]undefined的事实。使用response[x].year就像访问undefined.year一样,这是一个错误,所以使用:

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

作为警卫也会导致相同的错误,因为您仍在尝试访问response[x].year。你应该把它用作守卫:

if ( response[x] === undefined ) 

答案 1 :(得分:1)

就个人而言,我会做一些事情:

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