TypeError: $scope.array.reduce is not a function

TypeError: $scope.array.reduce is not a function

我有 3 个下拉列表加载与页面相关的属性。这些属性是:乐器、风格、得分。这些属性是从服务调用加载的。

使用工具的示例:

  //Get Instruments
  $http.get('/api/Instrument/GetAllInstruments').success(function (data, status, headers, config) {
      $scope.instruments = data;
  }).error(function (data, status, headers, config) {
      $scope.error = "An Error has occured while fetching Instruments!" + data;
  });

和html:

<select class="form-control" 
        name="recordInstrument" 
        data-ng-model="recordInstrument" 
        required
        data-ng-options="i.ID as i.Description for i in instruments">
    <option value="">-- Choose an Instrument --</option>
</select>

用户可以 select 列表中的记录,并在表单上加载该记录的值。这些值还包括为下拉菜单设置 selected 值的属性值。

当用户点击记录时,"edit" 函数被调用。该函数调用获取记录的服务,然后执行 if 语句以确定记录属性数组是否不为空。如果属性数组不为空,则它在数组上执行 forEach 循环,设置 ng-model,在本例中为 "$scope.recordInstrument" 以便 selected 为记录的下拉设置了默认值。如果数组为空,则将 ng-model 设置为 0,以将下拉列表重置为无记录 selected 值 "Choose an instrument".

这是那段代码: //编辑商店页面 $scope.edit = 函数 () {

      if (this.page.SPPreambleID === null || this.page.SPPreambleID === 0) {
          this.page.SPPreambleID = -1;
      }

      $http.get('/api/StorePage/GetStorePage?StorePageID=' +
        this.page.StorePageID +
        '&SPPreambleID=' +
        this.page.SPPreambleID).success(function (data) {

            $scope.updateShow = true;
            $scope.addShow = false;
            $scope.newpage = data;

            if (data.SPAttributeRefID.length > 0) {

                angular.forEach($scope.newpage.SPAttributeRefID, function (attribute, index) {

                    if (attribute == 1) {

                        $scope.instruments = $scope.instruments.reduce(function (result, instrument) {
                            result[instrument.ID] = instrument;
                            return result
                        }, {});

                        $scope.recordInstrument = $scope.instruments[data.AttributeID[0]].ID;
                    };

                    if (attribute == 2) {

                        $scope.style = $scope.styles.reduce(function (result, style) {
                            result[style.ID] = style;
                            return result
                        }, {});
                        $scope.recordStyle = $scope.styles[data.AttributeID[1]].ID;
                    };

                    if (attribute == 3) {
                        $scope.scoring = $scope.scorings.reduce(function (result, scoring) {
                            result[scoring.ID] = scoring;
                            return result
                        }, {});
                        $scope.recordScoring = $scope.scorings[data.AttributeID[2]].ID;
                    };
                });
            }
            else {
                $scope.recordInstrument = 0;
                $scope.recordStyle = 0;
                $scope.recordScoring = 0;
            }
        }).error(function () {
            $scope.error = "An Error has occured while Editing this Store Page!" + data;
        });
  }

在我放置 if 行之前上面的代码是有效的: 如果 ($scope.newpage.SPAttributeRefID.length > 0) { ... }

我添加 if 来检查数组长度的原因是因为一些记录没有下拉属性,如果我来自以前的记录,它有值,那么下拉将保留这些值从之前的记录来看。

添加 if 后,我开始收到指向 reduce 函数的错误,我不确定自己做错了什么。

我想寻求帮助来解决这个问题。

非常感谢。

看来你的问题就出在这里。

});
// you have a closing bracket missing
    }else {
        $scope.recordInstrument = 0;
    }
}).error(function () {
    $scope.error = "An Error has occured while Editing this Store Page!" + data;
});

固定

});
  };  // added a close bracket
    }else {
        $scope.recordInstrument = 0;
    }
}).error(function () {
    $scope.error = "An Error has occured while Editing this Store Page!" + data;
});

我最终不再使用 .reduce 了。 在发布新问题并获得有效答案后,我更改了代码以使用 for 循环:

这是替换 reduce 函数的代码片段:

if (attribute == 1) {

    var arrInstruments = $scope.instruments;
    var arrLength = arrInstruments.length;
    var result = {}
    for (var i = 0; i < arrLength; i++) {
        result[arrInstruments[i].ID] = arrInstruments[i];
    }
    $scope.recordInstrument = result[$scope.newpage.AttributeID[0]].ID;
}