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.

4
  • 3
    CrudService.getAllDataseems asynchronous to me..You can use .then Commented Apr 4, 2016 at 11:19
  • Share the implementation of getAllData Commented Apr 4, 2016 at 11:37
  • @RayonDabre I did it! See above in my code snippet. Commented Apr 4, 2016 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 As far as i know you should accept one of the answers given here, and see relevant question to your issue Commented Apr 4, 2016 at 12:56

2 Answers 2

1

I think you're looking for this:

console.log(vm.list.length);

or in your case

$log.info(vm.list.length);
Sign up to request clarification or add additional context in comments.

1 Comment

look to my comment above.
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

2 Comments

this doesn't work, because I'm getting Array[ ] and when I'm using .length it returns 0.
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

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.