Total Javascript Array Count:

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]  

It has several arrays on one page and I need to count them. Warning: I do not want to use length to know how many items in each array and yes how many arrays

For example, in the list above I would return the number 6 (total 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 generate this array several times and would like to count how many times this array was generated.

Does anyone know how to do this?

    
asked by anonymous 13.03.2017 / 20:03

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><divng-app="app" ng-controller="ctrl">
<button ng-click="countItems()">Clicar</button>
<div>{{count}}</div>
</div>
    
13.03.2017 / 20:45
0

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

P.S .: here counted 5 of the total, and it seems 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 .

    
14.03.2017 / 00:53