TypeError:$ scope.array.reduce不是函数 [英] TypeError: $scope.array.reduce is not a function

查看:85
本文介绍了TypeError:$ scope.array.reduce不是函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have 3 drop-down-lists that load attributes related to a page. These attributes are: instruments, style, scoring. The attributes are loaded from a service call.

使用工具的示例:

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

用户可以从列表中选择一条记录,在表单上,​​该记录的值为已加载。这些值还包括为下拉菜单设置选定值的属性值。

A user can select a record from a list, and on a form, the values of that record are loaded. These values also include the attribute values that set the selected value for the drop-downs.

当用户单击记录时,将调用编辑功能。该函数调用获取记录的服务,然后执行if语句来确定记录属性数组是否为空。如果属性数组不为空,则在数组上进行forEach循环,设置ng模型,在本例中为 $ scope.recordInstrument ,以便为记录的落下。如果数组为空,则将ng-model设置为0,以将下拉列表重置为无记录选择值选择工具。

When the user clicks the record, a, "edit" function gets called. The function calls a service that gets the record, then it does an if statement to determine if the record attribute array is not empty. If the attribute array is not empty then it does a forEach loop on the array, sets the ng-model, in this case "$scope.recordInstrument" so that the selected default value is set for the record's drop-down. If the array is empty, then it set the ng-model to 0, to reset the drop-down to the no record selected value "Choose an instrument".

此处就是这段代码:
//编辑商店页面
$ scope.edit = function(){

Here is that piece of code: //Edit Store Page $scope.edit = function () {

      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行之前起作用:
if($ scope.newpage.SPAttributeRefID.length> 0){
...
}

The code above was working before I put the if line: if ($scope.newpage.SPAttributeRefID.length > 0) { ... }

我添加if的原因是为了检查数组的长度是因为某些记录没有下拉属性,如果我来自以前的记录,它具有值,则下拉列表将保留之前记录中的那些值。

The reason I added the if to check the array's length was because some records do not have drop down attributes, and if I was coming from a previous record that it had values, then the drop downs will stay with those values from the record before.

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

After I added the if, I started getting the error pointing to the reduce function and I am not sure what I am doing wrong.

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

I would like to ask for help trying to resolve this issue.

非常感谢。

推荐答案

我最终不再使用.reduce。
发布新问题并获得有效答案后,我将代码更改为使用for循环: JavaScript:在不使用reduce库的情况下展平数组

I ended up not using .reduce anymore. I changed the code to use a for loop after posting a new question and getting a working answer: JavaScript: flatten an array without using reduce library

以下是替换reduce函数的代码段:

Here is the code snippet that replaces the reduce function:

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

这篇关于TypeError:$ scope.array.reduce不是函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆