Get array length from multi dimensional array

This question already has an answer here:

  • Length of a JavaScript object 32 answers

  • Object.keys(Accounts).length
    

    You are trying to get the "length" of an object, which does not exist. If this were an array, it would be enclosed in a bracket [].

    This is definitely an object, so in order to count the properties inside an object, you would have to use Object.keys() to contain the property names in an array and then count the elements from there.


    您可以使用for循环遍历对象中的所有元素和计数以获取数字:

    var Accounts = {
    'Account001' : { 'data1' : 'foo1', 'data2' : 'bar1' },
    'Account002' : { 'data1' : 'foo2', 'data2' : 'bar2' },
    'Account003' : { 'data1' : 'foo3', 'data2' : 'bar3' },
    'Account004' : { 'data1' : 'foo4', 'data2' : 'bar4' }
    };
    
    var count = 0;
    for(var k in Accounts) {
      if(Accounts.hasOwnProperty(k)) {
        count++;
      }
    }
    console.log(count);
    链接地址: http://www.djcxy.com/p/27266.html

    上一篇: 获取'命名'数组的长度?

    下一篇: 从多维数组获取数组长度