简体   繁体   English

如何使用JavaScript / AngularJS在控制台中显示数组长度

[英]How to display the Array length in console with JavaScript/AngularJS

I have a question about displaying the Array length in console.log(). 我对在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. 只有当我单击“数组”时,我才能在右侧看到该数组包含47个对象。

Is there a possibility to display in console: 是否可以在控制台中显示:

Array [47]

?

EDIT: 编辑:

When I'm using: 当我使用时:

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

it returns 0. 它返回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 看看文档

I think you're looking for this: 我认为您正在寻找这个:

console.log(vm.list.length);

or in your case 还是你的情况

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM