如何计算 JavaScript 数组对象?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2693021/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 01:28:34  来源:igfitidea点击:

How to count JavaScript array objects?

javascriptoopobject

提问by Nik Sumeiko

When I have a JavaScript object like this:

当我有一个这样的 JavaScript 对象时:

var member = {
    "mother": {
        "name" : "Mary",
        "age" : "48"
    },
    "father": {
        "name" : "Bill",
        "age" : "50"
    },
    "brother": {
        "name" : "Alex",
        "age" : "28"
    }
}

How to count objects in this object?!
I mean how to get a counting result 3, because there're only 3 objects inside: mother, father, brother?!

这个物体中的物体如何计数?!
我的意思是如何得到一个计数结果 3,因为里面只有 3 个对象:妈妈、爸爸、哥哥?!

If it's not an array, so how to convert it into JSON array?

如果它不是数组,那么如何将其转换为 JSON 数组?

回答by CMS

That's not an array, is an object literal, you should iterate over the own properties of the object and count them, e.g.:

那不是数组,是对象文字,您应该遍历对象自己的属性并对其进行计数,例如:

function objectLength(obj) {
  var result = 0;
  for(var prop in obj) {
    if (obj.hasOwnProperty(prop)) {
    // or Object.prototype.hasOwnProperty.call(obj, prop)
      result++;
    }
  }
  return result;
}

objectLength(member); // for your example, 3

The hasOwnPropertymethod should be used to avoid iterating over inherited properties, e.g.

hasOwnProperty方法应该用于避免迭代继承的属性,例如

var obj = {};
typeof obj.toString; // "function"
obj.hasOwnProperty('toString'); // false, since it's inherited

回答by TomZ

If you are using jquery on your page, this will work:

如果您在页面上使用 jquery,这将起作用:

$(member).toArray().length;

回答by Danieldms

You can try this code, it works perfectly in a browser:

你可以试试这个代码,它在浏览器中完美运行:

Object.keys(member).length;

回答by gnarf

That is not an array, it is an object literal.

那不是一个数组,它是一个对象字面量。

You can iterate the objects properties and count how many it owns:

您可以迭代对象属性并计算它拥有的数量:

var count = 0;
for (var k in obj) {
  // if the object has this property and it isn't a property
  // further up the prototype chain
  if (obj.hasOwnProperty(k)) count++;
}

回答by micheledallachiara

You can use the Object.keys() method that returns an array of a given object's own enumerable properties:

您可以使用 Object.keys() 方法返回给定对象自己的可枚举属性的数组:

Object.keys(member).length;

回答by LittleFi.F

var member = {
        "mother": {
            "name" : "Mary",
            "age" : "48"
        },
        "father": {
            "name" : "Bill",
            "age" : "50"
        },
        "brother": {
            "name" : "Alex",
            "age" : "28"
        }
    }
    console.log('member key size:' + Object.keys(member).length);

ref a good guy : https://stackoverflow.com/questions/5223/length-of-a-javascript-object

回答by Peter Bailey

Here's how I'd do it

这是我的方法

function getObjectLength( obj )
{
  var length = 0;
  for ( var p in obj )
  {
    if ( obj.hasOwnProperty( p ) )
    {
      length++;
    }
  }
  return length;
}

回答by Ivijan Stefan Stipi?

In my practice I work like this:

在我的实践中,我是这样工作的:

/*
*   $.count(array) -  Returns the number of elements in an array (immproved equivalent to .length property). 
*/
$.count = function (array){
    if(array.length)
        return array.length;
    else
    {
        var length = 0;
        for ( var p in array ){if(array.hasOwnProperty(p)) length++;};
        return length;
    }
}