迭代对象数组并更改多个值

时间:2017-04-27 15:44:41

标签: javascript jquery angularjs

我有一个函数,我发送一个对象wholesaler。有一组对象wholesalers,其中包含多个wholesaler个对象。

enter image description here

我需要将刚刚进入的批发商的订单号增加1,然后找到具有新值的当前ORDER并减去1的批发商对象。

    $scope.wholesalers = [];
    $scope.downWholesaler = function (wholesaler) {
        if (wholesaler.id > 0) {
            var orderNumber = wholesaler.ORDER;
            var orderBelowNumber = orderNumber + 1;
            angular.forEach($scope.wholesalers, function (myWholesaler) {
                if (myWholesaler.ORDER === orderNumber) {
                    // raise current order +1
                    var add = orderNumber + 1
                    myWholesaler.ORDER = add;
                }
                //setting it
                if (myWholesaler.ORDER === orderBelowNumber) {
                    //lower one with order below number -1
                    var subtract = orderNumber - 1;
                    myWholesaler.ORDER = subtract;
                }
            });
            console.log($scope.wholesalers);
        }
    };

上面我正在努力做到这一点。我浏览$scope.wholesalers并增加当前对象的订单号,然后将其旁边的一个(ORDER +1)减少一个。

但是ORDER正在下降,下一个对象根本没有改变。我做错了什么,如何解决?

编辑:没有最新的代码

1 个答案:

答案 0 :(得分:0)

修正:

    $scope.downWholesaler = function (wholesaler) {
        if (wholesaler.id > 0) {
            var add = parseInt(wholesaler.ORDER) + 1

            angular.forEach($scope.wholesalers, function (myWholesaler) {
                if (myWholesaler.ORDER == add) {
                    myWholesaler.ORDER = parseInt(myWholesaler.ORDER) - 1;
                }
            });

            wholesaler.ORDER = add;

            console.log($scope.wholesalers);
        }
    };