Count total of arrays in Javascript:

Asked

Viewed 993 times

0

[Object, Object, Object, Object]  
[Object, Object, Object, Object]  
[Object, Object, Object, Object]  
[Object, Object, Object, Object]  
[Object, Object, Object, Object]  
[Object, Object, Object, Object]  

Has several arrays on a page and need to count them. Heed: I don’t want to use the length to find out how many items there are in each array and know how many arrays has the whole.

For example, in the list above I would return the number 6 (total of arrays).
I have a fixed test array:

$scope.tickets = [{
    name: 'Pista Inteira - Lote 01',
    items: [{
      type: 'Homem',
      value: '100,00',
      quantity: 0
    }, {
      type: 'Mulher',
      value: '80,00',
      quantity: 0
    }]
  },
  {
    id: 1,
    name: 'Pista (PNE)',
    items: [{
      type: 'Homem',
      value: '100,00',
      quantity: 0
    }, {
      type: 'Mulher',
      value: '80,00',
      quantity: 0
    }]
  },
  {
    id: 2,
    name: 'Camarote',
    items: [{
      type: 'Homem',
      value: '100,00',
      quantity: 0
    }, {
      type: 'Mulher',
      value: '80,00',
      quantity: 0
    }]
  },
  {
    id: 3,
    name: 'Lounge',
    items: [{
      type: 'Homem',
      value: '100,00',
      quantity: 0
    }, {
      type: 'Mulher',
      value: '80,00',
      quantity: 0
    }]
  },
];

I run this array several times and would like to count how many times this array has been generated.

Can anyone tell me how to do it?

  • has the example of this set of arrays?

  • And where are these array on the page? Where do these come from array?

  • I’ll edit the question

  • 1

    you want the amount of arrays inside each key items?

  • 1

    How are these arrays generated? Anything encapsulates them in another array using push and take the length of it

  • I don’t see multiple arrays, I see an array of objects that contains array.

  • @Using no proposed solutions has served you?

Show 2 more comments

2 answers

0


var app = angular.module("app", []);
app.controller("ctrl", ["$scope", function($scope) {
  $scope.count = 0;
  $scope.countItems = function()
  {
      $scope.count = 0;
      angular.forEach($scope.tickets, function(a){
         $scope.count += a.items.length;  
      });      
  }
  $scope.tickets = [{
      name: 'Pista Inteira - Lote 01',
      items: [{
        type: 'Homem',
        value: '100,00',
        quantity: 0
      }, {
        type: 'Mulher',
        value: '80,00',
        quantity: 0
      }]
    },
    {
      id: 1,
      name: 'Pista (PNE)',
      items: [{
        type: 'Homem',
        value: '100,00',
        quantity: 0
      }, {
        type: 'Mulher',
        value: '80,00',
        quantity: 0
      }]
    },
    {
      id: 2,
      name: 'Camarote',
      items: [{
        type: 'Homem',
        value: '100,00',
        quantity: 0
      }, {
        type: 'Mulher',
        value: '80,00',
        quantity: 0
      }]
    },
    {
      id: 3,
      name: 'Lounge',
      items: [{
        type: 'Homem',
        value: '100,00',
        quantity: 0
      }, {
        type: 'Mulher',
        value: '80,00',
        quantity: 0
      }]
    },
  ];
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app="app" ng-controller="ctrl">
<button ng-click="countItems()">Clicar</button>
<div>{{count}}</div>
</div>

0

This solution below uses a recursive function and checks the properties of each object. If you pass primitive values in the for... in will give a stack overflow with strings (because at least in Ecmascript 5 there is getter to read each character, as to read each item of an array).

P.S.: here counted 5 of the total, and it appears to be.

// Quantidade de `Array`'s contadas.
var amount = 0;

function count(value) {
    var k;

    if (typeof value === 'object') {

        if (value instanceof Array) {
            // ou: value.constructor === Array
            ++amount;
        }

        for (k in value) {
            count(value[k]);
        }
    }
}

On the condition of checking if the value is an array... instanceof will work with classes that inherit Array.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.