Angularjs:如何拆分任何数组列表的一半

时间:2017-10-27 14:43:16

标签: angularjs ionic-framework

我目前将json中的列表拆分为前10个,第二个是11到20.但是,我希望将列表的任何长度拆分为一半。我该怎么办?

这是我的代码

  function loadTop() {
    $webServicesFactory.get($marketProvider[$scope.currentMarket].topGetURL, {AnonymousToken: $marketProvider[$scope.currentMarket].token}).then(
      function success(response) {
        $scope.top = response.Stock.slice(0, 10);
        $scope.low = response.Stock.slice(10, 20);
        console.log($scope.top);
        loadStockCount();
        loadTop();
        loadLow();
        $ionicLoading.hide();
      },
      function error(error) {
        $ionicLoading.hide();
      }
    );
  }

1 个答案:

答案 0 :(得分:0)

var numStocks = response.Stock.length;
var halfStocks = Math.round(numStocks / 2);
$scope.top = response.Stock.slice(0, halfStocks);
$scope.low = response.Stock.slice(halfStocks, numStocks);