0

I have a question about displaying the Array length in console.log().

Here is a simple example:

vm.list = CrudService.getAllData();

function getAllFonds() {
   return ResService.names.query(
      succResp,
      errResp
   );
}

function succResp(resp) {
   return resp;
}

function ResService($resource, baseUrl) {
    return {
        names: $resource(baseUrl + '/api/list/:Id', {
            Id: '@Id'
        }, {
            'update': {
                method: 'PUT'
            }
        }),
    ....}
 }

$log.info(vm.list);

When I'm opening the console, there will be display only:

Array [ ]

Only when I click on "Array" then I see on the right side that the array contains 47 objects.

Is there a possibility to display in console:

Array [47]

?

EDIT:

When I'm using:

$log.info(vm.list.length);

it returns 0.

yuro
  • 2,189
  • 6
  • 40
  • 76
  • 3
    `CrudService.getAllData`seems `asynchronous` to me..You can use `.then` – Rayon Apr 04 '16 at 11:19
  • Share the implementation of `getAllData` – Rayon Apr 04 '16 at 11:37
  • @RayonDabre I did it! See above in my code snippet. – yuro Apr 04 '16 at 11:50
  • 1
    This problem you're having is not actually relevant to Array.length property. Your issue origins in Asynchronous call. The fact that your browser cleverly populates the logged variable in the console is a feature of your browser, and itself is populating it after your async call. You need to implement your logging after finishing your async call. I myself asked such a question [here](http://stackoverflow.com/questions/25499182/chromium-logs-wrong-javascript-values) As far as i know you should accept one of the answers given here, and see relevant question to your issue – Slytherin Apr 04 '16 at 12:56

2 Answers2

1

I think you're looking for this:

console.log(vm.list.length);

or in your case

$log.info(vm.list.length);
FvB
  • 713
  • 6
  • 15
0

Sure there is

console.log(yourArray.length);

Or if you insist on that format you can do

console.log('Array['+yourArray.length+']');

Take a peek at the docs

Slytherin
  • 474
  • 3
  • 17
  • this doesn't work, because I'm getting `Array[ ]` and when I'm using `.length` it returns 0. – yuro Apr 04 '16 at 11:22
  • 1
    In that case you are trying to log length of unpopulated array. Refer to Rayons comment under your question. You have to log the length when your async call gets delivered – Slytherin Apr 04 '16 at 11:23