1

I currently split a list in json to first 10 and the second is 11 to 20. However, I want to split it to half for any length of the list. How can I do it?

This is my code

  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();
      }
    );
  }
4
  • I think you are searching for counting number of objects in object Commented Oct 27, 2017 at 14:45
  • 3
    key word is length, just use your_list.length / 2 Commented Oct 27, 2017 at 14:47
  • 1
    Possible duplicate of Splice an array in half, no matter the size? Commented Oct 27, 2017 at 15:48
  • this question has really nothing to do with angularj or ionic-framework. It is a JavaScript question involving arrays. Commented Oct 27, 2017 at 15:49

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.