AngularJS无法读取未定义的属性“ forEach”

时间:2019-04-27 11:39:26

标签: javascript angularjs google-chrome debugging syntax-error

此代码在开发者控制台中导致错误:

$scope.AddCandidatesToCompannies = function()
{
    console.table($scope.candidates);
    Object.entries($scope.candidates).forEach(candidate =>
    {
        candidate.jobs.forEach(job =>
        {
            var company_id = job['company_id'];

            Object.entries($scope.companies).forEach(company =>
            {
                if (company['company_id'] == company_id)
                {
                    console.log('match'); 
                };
            });
        });
    });
};   // AddCandidatesToCompannies()

$scope.candidates 确实 包含数据,并且 一个对象;它非常复杂,所以我不会在上面提出问题,但是她是概述图片

enter image description here

语句Object.entries($scope.candidates).forEach(candidate =>给出

  

angular.min.js:83 TypeError:无法读取未定义的属性'forEach'
      在mapController.js:138
      在Array.forEach()
      在a。$ scope.AddCandidatesToCompannies(mapController.js:136)
      位于$ scope.ProcessSearchResults(mapController.js:161)
      在C(angular.min.js:91)
      在C(angular.min.js:91)
      在angular.min.js:92
      在h。$ eval(angular.min.js:100)
      在h。$ digest(angular.min.js:98)
      在h。$ apply(angular.min.js:101)处
      在f(angular.min.js:66)
      在K(angular.min.js:70)
      在XMLHttpRequest.y.onreadystatechange(angular.min.js:71)

有人可以看到我做错了吗?

1 个答案:

答案 0 :(得分:1)

Object.entries返回带有键,值对的数组中的数组。 看起来您想拥有类似的功能 Object.values($scope.candidates)

示例:

Object.entries($scope.candidates) == [[45,{candidate_id:45}],[46,{candidate_id:46}]]
Object.values($scope.candidates) == [{candidate_id:45}, {candidate_id: 46}]