Javascript 如果使用相同的函数,如何清空数组

Javascript 如果使用相同的函数,如何清空数组,javascript,angularjs,Javascript,Angularjs,我在代码中的三个位置使用一个函数。我不喜欢重复代码。下面是获取所选项目预算的函数 var getBudgetForAll = function(){ var bId = []; angular.forEach($scope.myCartItems, function (value, key) {

我在代码中的三个位置使用一个函数。我不喜欢重复代码。下面是获取所选项目预算的函数

var getBudgetForAll = function(){
                            var bId = [];
                                        angular.forEach($scope.myCartItems, function (value, key) {
                                            bId.push(value.id);
                                        });
                                        $http.get(serviceBase + 'xxxx/' + $rootScope.user_id + '/xxxxx/calculate/max-budget/from/' +
                                                $scope.ctrl.picker4.date + '/to/' + $scope.ctrl.picker5.date + '?smartb=' + benchId.join())
                                                .success(function (maxBudget)
                                                {
                                                    $scope.maxBudget = maxBudget;

                                                });

                        }
现在,我在代码的三个地方使用这个函数

 $scope.allSelectedb = function () {
                             var onebyOneAddedB = getOnebyOneAddedB($scope.myCartItems);
                                $scope.myCartItems = [];
                                $scope.maxBudget = []; //here i try empty maxBudget
                            if ($scope.filteredCountries !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                 addNonDuplicateItems($scope.filteredCountries); 
                                 addNonDuplicateItems(onebyOneAddedB);
                                 getBudgetForAll(); //here i call function

                            }
                            if ($scope.filteredStates !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                $scope.myCartItems = $scope.filteredStates;
                                addNonDuplicateItems($scope.filteredStates);
                                addNonDuplicateItems(onebyOneAddedB);
                                getBudgetForAll(); //here i call function
                            }
                            if ($scope.filteredCities !== undefined) {
                                $scope.maxBudget = []; //here i try empty
                                $scope.myCartItems = [];
                                addNonDuplicateItems($scope.filteredCities);
                                addNonDuplicateItems(onebyOneAddedB);
                                getBudgetForAll(); //here i call function
                            }
                        };
我有三个选项,首先选择country,然后按GET ALL,在$scope.maxBudget中,我可以为country中的所有项目获取最大预算。如果我想要州,在选择国家后,我们需要选择州,然后按GET ALL,然后获取州内项目的最大预算…城市也是如此。 问题: 选择国家/地区时-最高预算正常 选择州时-GET ALL按钮再次为国家和州发出GET请求,并设置为$scope.maxBudget,有时为国家预算,有时为州预算。
在选择城市时-与州时相同,在“获取全部”时,我获得了国家、州和城市的请求,并设置了$scope。maxBudget有时用于国家、州和城市。在我的代码中,您可以看到,我尝试执行empty$scope.maxBudget,但这不起作用。

thnx@rajesh获取您的提示。我很容易解决这个问题,我只需要

getBudgetForAll()


出自if:)

@Rajesh thnx,请发表评论。你能告诉我更多的细节吗?ThnxI只阅读了标题并作了相应的评论。据我所知,首先在
allSelectedb
中有冗余代码。其次,您应该使用
if。。否则如果。。。如果。。。如果。。。如果…