How to count JavaScript array objects?

97,269

Solution 1

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 hasOwnProperty method should be used to avoid iterating over inherited properties, e.g.

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

Solution 2

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

Object.keys(member).length;

Solution 3

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

$(member).toArray().length;

Solution 4

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

Object.keys(member).length;

Solution 5

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++;
}
Share:
97,269
Nik Sumeiko
Author by

Nik Sumeiko

JavaScript hacker and Frontend infrastructure engineer. I work freelance with teams at Siemens, Sony, Samsung, IDEO and other companies. Love to build software that scales.

Updated on July 09, 2022

Comments

  • Nik Sumeiko
    Nik Sumeiko almost 2 years

    When I have a JavaScript object like this:

    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?!

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

  • James
    James about 14 years
    Might be worth checking for Mozilla's __count__ property before iterating over the object...
  • Christian C. Salvadó
    Christian C. Salvadó about 14 years
    @J-P: __count__ is marked as obsolete for Gecko 1.9.3, (in Gecko 1.9.3a5pre, (Firefox 3.7a5pre) it doesn't exist anymore)
  • Christian C. Salvadó
    Christian C. Salvadó about 14 years
    @J-P, something that you could check before iterating, is the existence of the new ECMAScript 5th Edition Object.keys method, ES5 is starting to be implemented by all major browser vendors, something like this: if (typeof Object.keys === "function") return Object.keys(obj).length;
  • Frederico
    Frederico about 4 years
    I feel like this is overdoing. If he's simply counting the number of properties in a JSON, relying on Object.keys(memebers) should be enough.
  • Dale K
    Dale K about 4 years
    Seems identical to an earlier answer.
  • J A S K I E R
    J A S K I E R over 3 years
    Finally the solution for "Inside" methods, instead of documentation for Querries)